0

I have the following CURL request Can anyone please confirm me what would be the subesquest HTTP Request

curl -H "Authorization: Bearer <access token>" --data-urlencode "model=" --data-urlencode "imageurl.img.com" apiurl.api.com

What I tried:

HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);


conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer " + token);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

String payload = "model=\"\"&url=" + url;

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();

The problem is I keep getting a 403 HTTP Error code. It works with CURL, so I might not be converting it right.

What can I do to make the conversion?

@EDIT

API: http://wiki.vg/Mojang_API#Change_Skin Mojang's Example:

curl -H "Authorization: Bearer <access token>" --data-urlencode "model=" --data-urlencode "url=http://assets.mojang.com/SkinTemplates/steve.png" https://api.mojang.com/user/profile/<uuid>/skin
MoisesCol
  • 303
  • 3
  • 21
  • I mean the better use exist library in java, for example: https://github.com/libetl/curl – Grzesiek Feb 22 '17 at 18:29
  • I notice a comment at http://wiki.vg/Talk:Mojang_API: “How exactly should the payload look like? Whatever I send (even garbage data) I get back a 403 HTTP response with `{"error":"Forbidden","errorMessage":"Current IP not secured"}`. Have you tried running the suggested `curl` command and does it give you the response you expect? Or does it instead also give you a 403? You can prepend `-i` to it to see the status code and headers from the response: `curl -i -H "Authorization: Bearer "…` – sideshowbarker Feb 23 '17 at 03:37

2 Answers2

1

The curl invocation in the question causes the POST body to be model=&imageurl.img.com:

$ curl --trace-ascii - -H "Authorization: Bearer <access token>" \
  --data-urlencode "model=" --data-urlencode "imageurl.img.com" https://example.com

=> Send header, 183 bytes (0xb7)
0000: POST / HTTP/1.1
0011: Host: example.com
0024: User-Agent: curl/7.51.0
003d: Accept: */*
004a: Authorization: Bearer <access token>
0070: Content-Length: 23
0084: Content-Type: application/x-www-form-urlencoded
00b5:
=> Send data, 23 bytes (0x17)
0000: model=&imageurl.img.com
== Info: upload completely sent off: 23 out of 23 bytes

Specifically that curl invocation doesn’t include a url= param.

In contrast, the Java seems to cause the POST body to be model=""&url=imageurl.img.com—assuming other code (not shown in the question) sets the url variable to imageurl.img.com.

I guess it could instead be that there’s no code which sets theurl variable to imageurl.img.com. There’s no way to tell from just the snippet in the question.

Also the curl invocation sends the header Accept: */* while the Java code instead sends the header Accept: application/json. I wouldn’t think that’d cause a 403 but who knows.

Also, the curl invocation sends a User-Agent: curl/7.51.0 header, but it’s not clear if the Java code is causing a User-Agent header to be sent. I’d assume it is, but again, who knows.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
-1

You are setting properties but should specify headers.

Egor Zhuk
  • 274
  • 1
  • 7