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
2
  • I mean the better use exist library in java, for example: github.com/libetl/curl Commented Feb 22, 2017 at 18:29
  • I notice a comment at 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 <access token>"… Commented Feb 23, 2017 at 3:37

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The actual API I'm trying to use can be found here wiki.vg/Mojang_API#Change_Skin. I'll update the question with their example
Their example is what I'm sortta trying to replicate with Java
-1

You are setting properties but should specify headers.

2 Comments

and how can i do that?
@MoisesCol seems I'm wrong. Please check stackoverflow.com/questions/12732422/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.