質問

I am working with the Snapchat API to make a Java Client. I am using an endpoint which takes the following parameters from an HTTP POST:

{
username: snapchat username,
timestamp: UNIX timestamp,
media_id: random string,
type: 0,
req_token: request token,
data: encrypted data
}

I have no problem generating the params hash, and I have the data as a File object.

In Python I have confirmed that the following works:

f = open('encrypted.jpg')
params = { ... all params besides data ... }
files = { 'data' : f }
r = requests.post(path, params, files=files)

That Python code gets me a 200. I am using tokens and data/files generated by Java code, so the data sent is identical.

In Java I am doing the following with UniRest:

Map<String, Object> params = ... same params ...;
File f = new File('encrypted.jpg');
HttpRequestWithBody req = Unirest.post(path);
req.fields(params);
req.field("data", f);
HttpResponse<String> resp = req.asString();

However this gives me a 500 response from the server. How can I write Java that emulates the Python exactly? Or how can I snoop my own network traffic to see the difference in what the code for each is doing? Seems crazy to me that one works and the other does not.

役に立ちましたか?

解決

try to chain methods, i.e.

req = Unirest.post(path).fields(params).field("data", f);

or change the lines:

req = req.fields(params); 
req = req.field("data", f);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top