Edit: I think I've figured out how to do the binary data part. Double check it in the code, but I'm pretty sure I've got it right. Now I'm getting a new error when trying to complete the upload as described in the Vimeo API documentation
Edit 2: Added .debug() to the OAuthService and updated the output.
Original Question: I'm trying to upload a video to Vimeo using the Vimeo API (Streaming Method). I'm using scribe to authorize my app, get my access token and prepare for the video upload. I just don't know what to do at the point the Vimeo API documentation says "binary data of your file here" in this example of a HTTP request for the PUT:
PUT http://1.2.3.4:8080/upload?ticket_id=abcdef124567890 HTTP/1.1
Host: 1.2.3.4:8080
Content-Length: 339108
Content-Type: video/mp4
.... binary data of your file here ....
I can get the ticket and headers fine. It's just, what do I do to insert the binary data of my file?
Notes:
It's a desktop app in Java
It's a small video file I'm testing
Here's my code for the put (remember, I'm using scribe)
// Setup File (line 52)
File testUp = new File("C:/Users/Kent/Desktop/test.mp4");
String contentLength = Long.toString(testUp.length());
System.out.println("The content length is: " + contentLength);
byte[] fileBytes = ByteStreams.toByteArray(new FileInputStream(testUp));
// Upload file (line 58)
request = new OAuthRequest(Verb.PUT, endpoint);
request.addHeader("Content-Length", contentLength);
request.addHeader("Content-Type", "video/mp4");
request.addPayload(fileBytes);
response = signSendAndPrint(service, accessToken, request, "Upload PUT: " + endpoint);
// Check response code is valid (line 65)
if (response.getCode() != 200) {
System.out.println("The response was not 200! It was: " + response.getCode());
return;
}
// Verify the upload (line 71)
request = new OAuthRequest(Verb.PUT, endpoint);
request.addHeader("Content-Length", "0");
request.addHeader("Content-Range", "bytes */*");
response = signSendAndPrint(service, accessToken, request, "Verify Upload PUT: " + endpoint);
// Check response code is valid (line 77)
if (response.getCode() != 308) {
System.out.println("The response was not 308! It was: " + response.getCode());
return;
}
// Complete Upload (line 83)
request = new OAuthRequest(Verb.PUT, endpoint);
request.addQuerystringParameter("method", "vimeo.videos.upload.complete");
Response completeResponse = signSendAndPrint(service, accessToken, request, "complete"); // This is line 86, it's the second to top level of my code that breaks the process.
//Set video info (line 88)
setVimeoVideoInfo(completeResponse, service, accessToken, vimeoAPIURL);
}
/**
* Signs the request on the service, prints information on the request, sends the request, prints
* information on the response and returns the response
*
* #param service
* #param accessToken
* #param request
* #param description
* #return
*/
private static Response signSendAndPrint(OAuthService service, Token accessToken, OAuthRequest request, String description) {
service.signRequest(accessToken, request);
printRequest(request, description + " Request");
Response response = request.send(); //This is line 105. It's the top level of my code that breaks the process
printResponse(response, description + " Response");
return response;
}
Output of the signSendAndPrint(OAuthService service, Token accessToken, OAuthRequest request, String description) method and on the OAuthService.debug():
Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Request
Headers: {Authorization=OAuth oauth_signature="MTPIVFfGVUQn4QswNV6av4CjzJw%3D", oauth_version="1.0", oauth_nonce="-606493399", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="MY_CONSUMER_KEY", oauth_token="MY_OAUTH_TOKEN", oauth_timestamp="1332428103", Content-Length=15125120, Content-Type=video/mp4}
Verb: PUT
Complete URL: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e
Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Response
Code: 200
Headers: {null=HTTP/1.1 200 OK, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0}
Body:
signing request: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e
setting token to: Token[87bddf1382ac9f423d4b7c4166bdf0b2 , fdae7a033c7e1c932abce533627d6045124e8593]
generating signature...
base string is: PUT&http%3A%2F%2F174.129.155.54%3A8080%2Fupload&oauth_consumer_key%3DMY_CONSUMER_KEY%26oauth_nonce%3D1585934110%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1332428110%26oauth_token%3DMY_OAUTH_TOKEN%26oauth_version%3D1.0%26ticket_id%3Da64ed67b4aefdc35d18aec6cfa0b7c5e
signature is: bhWu7IX9JKEEn/ULcpJECEuwqOc=
appended additional OAuth parameters: { oauth_signature -> bhWu7IX9JKEEn/ULcpJECEuwqOc= , oauth_version -> 1.0 , oauth_nonce -> 1585934110 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> MY_CONSUMER_KEY, oauth_token -> MY_OAUTH_TOKEN, oauth_timestamp -> 1332428110 }
using Http Header signature
Verify Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Request
Headers: {Authorization=OAuth oauth_signature="bhWu7IX9JKEEn%2FULcpJECEuwqOc%3D", oauth_version="1.0", oauth_nonce="1585934110", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="MY_CONSUMER_KEY", oauth_token="MY_OAUTH_TOKEN", oauth_timestamp="1332428110", Content-Length=0, Content-Range=bytes */*} *///Note, this is not part of the output, I just added */// so the rest of it doesn't appear commented out.
Verb: PUT
Complete URL: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e
Verify Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Response
Code: 308
Headers: {null=HTTP/1.1 308 Resume Incomplete, Range=bytes=0-15125119, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0}
Body:
signing request: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e&method=vimeo.videos.upload.complete
setting token to: Token[87bddf1382ac9f423d4b7c4166bdf0b2 , fdae7a033c7e1c932abce533627d6045124e8593]
generating signature...
base string is: PUT&http%3A%2F%2F174.129.155.54%3A8080%2Fupload&method%3Dvimeo.videos.upload.complete%26oauth_consumer_key%3DMY_CONSUMER_KEY%26oauth_nonce%3D3111236130%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1332428110%26oauth_token%3DMY_OAUTH_TOKEN%26oauth_version%3D1.0%26ticket_id%3Da64ed67b4aefdc35d18aec6cfa0b7c5e
signature is: vXlQ6OUKms8eHan+wEBO2HXBn/M=
appended additional OAuth parameters: { oauth_signature -> vXlQ6OUKms8eHan+wEBO2HXBn/M= , oauth_version -> 1.0 , oauth_nonce -> 3111236130 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> MY_CONSUMER_KEY, oauth_token -> MY_OAUTH_TOKEN, oauth_timestamp -> 1332428110 }
using Http Header signature
complete Request
Headers: {Authorization=OAuth oauth_signature="vXlQ6OUKms8eHan%2BwEBO2HXBn%2FM%3D", oauth_version="1.0", oauth_nonce="3111236130", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="MY_CONSUMER_KEY", oauth_token="MY_OAUTH_TOKEN", oauth_timestamp="1332428110"}
Verb: PUT
Complete URL: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e&method=vimeo.videos.upload.complete
Exception in thread "main" org.scribe.exceptions.OAuthException: Problems while creating connection.
at org.scribe.model.Request.send(Request.java:70)
at org.scribe.model.OAuthRequest.send(OAuthRequest.java:12)
at autouploadermodel.VimeoTest.signSendAndPrint(VimeoTest.java:105)
at autouploadermodel.VimeoTest.main(VimeoTest.java:86)
Caused by: java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:723)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:589)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:720)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:589)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1319)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at org.scribe.model.Response.<init>(Response.java:28)
at org.scribe.model.Request.doSend(Request.java:110)
at org.scribe.model.Request.send(Request.java:62)
... 3 more
Java Result: 1
You need to append two other parameters when completing the upload. Vimeo API documentation says you need three parameters for vimeo.videos.upload.complete: filename, oauth_token and ticket_id. scribe takes care of the oauth_token for you.
Furthermore, you have to call the original endpoint, not the one obtained when requesting a ticket.
Related
i need to insert # symbol in the api request. official documentation transforms https://api.brawlstars.com/v1/players/#2JV9PR99Y to https://api.brawlstars.com/v1/players/%232JV9PR99Y.
so if i send request with %23 i will get 404 Not Found: [{"reason":"notFound","message":"Not found with tag %232JV9PR99Y"}] but with postman i can send requests with %23 replace #. if if send request with # it just returns null json body.
my code:
public Player showPlayerInfo(String playerTag) {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization","Bearer " + bearerToken);
HttpEntity<String> request = new HttpEntity<String>(headers);
String url = "https://api.brawlstars.com/v1/players/%232JV9PR99Y"; // testing
System.out.println(url);
ResponseEntity<Player> response = restTemplate.exchange(url, HttpMethod.GET, request, Player.class);
return response.getBody();
}
tried to transform link using java.lang URI and URL. also just delete # or %23 from url. api doesnt get it
I am using swagger codegen to generate the controller for me. And there is a if statement there but it seems to me it is not necessary. Thank you.
String accept = request.getHeader("Accept");
if (accept != null && accept.contains("application/json"))
Your swagger document must have something like below to API method:
responses:
"200":
description: OK
content:
application/json:
As per this, the response of your API is of type application/json:. But additionally in future, if the server decides to produce some other type of response also as below:
responses:
"200":
content:
image/svg+xml:
schema:
type: string
application/json:
schema:
$ref: "#/components/schemas/MyDto"
In this case to decide the response type in response Accept parameter would be necessary. So in my opinion there are 2 reasons for this condition to generate:
That Client and Server are in same contract for returning content.
If tomorrow new content type is added old code is not breaking.
Content-Type: it is a header that tells the format of data sent in HTTP messages (both requests and responses).
Accept: it is a header that is placed in a request when requested from a browser (client) to a web server.
This Accept header simply means that I(client) will only allow you this data type as a response.
If the server supports multiple data types, The server determines a data type of response using a accept header like this,
#GetMapping("/someresources")
public ResponseEntity<String> getSomeresources(#RequestHeader("accept") String accept) {
List<SomeResource> someresources = someService.someresources();
//for react app
if(accept.contains("application/json")) {
SomeresourcesRepresentation representation = new SomeresourcesRepresentation(someresources);
String serialziedRepresentaiton = jsonSerializer.serialize(representation);
ResponseEntity<String> response = ResponseEntity
.status(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON)
.body(serialziedRepresentaiton);
return response;
}
//for web browser
if(accept.contains("text/html")) {
String html = "<!doctype html>"
+ "<html>"
+ "<head>"
+ "<meta charset='UTF-8'>"
+ "<title>summary</title>"
+ "</head>"
+ "<body>"
+ "someresources size : "+someresources.size()
+ "</body>"
+ "</html>";
ResponseEntity<String> response = ResponseEntity
.status(HttpStatus.OK)
.contentType(MediaType.TEXT_HTML)
.body(html);
return response;
}
return ResponseEntity.notFound().build();
}
API Headers have two parameter Content-Type=application/json and also accesstoken = "some_token"
I trying to automate the API using Rest assured but not successful.
Below is the code
RestAssured.baseURI = prop.getProperty("serviceurl1");
//2. define the http request:
RequestSpecification httpRequest = RestAssured.given()
.filter(new ResponseLoggingFilter())
.filter(new RequestLoggingFilter());
JSONObject requestParams = new JSONObject();
requestParams.put("longitude", eLongitude);
requestParams.put("latitude", eLaititude);
requestParams.put("country", eCity);
httpRequest.headers("Content-Type", "application/json");
httpRequest.headers("accesstoken", "some_token.");
httpRequest.body(requestParams.toJSONString());
int statusCode = response.getStatusCode();
System.out.println("the status code is: "+ statusCode);
Assert.assertEquals(statusCode, TestUtil.RESPONSE_CODE_200);
System.out.println("the status line is: "+ response.getStatusLine());
//6. get the headers:
Headers headers = response.getHeaders();
System.out.println(headers);
String contentType = response.getHeader("Content-Type");
System.out.println("the value of content-type header is: "+ contentType);
String contentLength = response.getHeader("Content-Length");
System.out.println("the value of Content-Length header is: "+ contentLength);
Getting error message as "Provide Application Token" and 404 error code display.
Your httpRequest.headers("accesstoken", "kggkgkgkgketdfgxgcccvcdftfty."); is wrong. It should be:
httpRequest.headers("Authorization", "Bearer "+token);
can you try this once
Response resp = given().when().contentType(ContentType.JSON).header("accesstoken", "token").body(body).put("url");
You can pass the HashMap as body
These are the issues I can think of
This might be an internal API and it is expecting "Provide Application Token" and not the "accesstoken"
The error code you are getting is 404. So either the service is down or the URL you are using is not correct.
Hope this helps :)
I am sending a JSON request via http post. Backend requires me to send two headers, namely - Authorization and Content-Type. Then a raw JSON request as body. I send the request via Post method via Postman tool and the message gets through successfully. But when I send the same request with same headers via code, I get a bad request - Error.
I minimized the scope of testing to check.
I have sent only the two headers via Postman and here's the response:
Headers
Authorization: "bearer " + token. The structure is "bearer" keyword + space + Okta token.
Content-Type: application/json
Response:
Headers
Content-Type →application/json; charset=utf-8
Strict-Transport-Security →max-age=31536000
X-Content-Type-Options →nosniff
Request-Context →appId=cid-v1:dc220dda-e515-4dd4-b324-c078a1b8fac4
Date →Sun, 26 May 2019 13:23:01 GMT
Content-Length →29
Body
{
"Message": "No Request Body"
}
But when I send the same thing via code, I get a Bad-Request Error. Please help :(
HttpClient httpclient1;
HttpPost httppost1;
//ArrayList<NameValuePair> postParameters1 = null;
httpclient1 = HttpClientBuilder.create().build
httppost1 = new HttpPost(Constants.URL);
// httppost1.setEntity(new UrlEncodedFormEntity(postParameters1,"UTF-8"));
// httppost1.setEntity(new StringEntity(request,"UTF-8"));
httppost1.setHeader("Authorization", token);
httppost1.setHeader("Content-Type", "application/json");
// httppost1.setHeader("Content-Length", strlength);
// httppost1.setHeader("Accept", "application/json");
// StringEntity entity = new StringEntity(request,ContentType.APPLICATION_JSON);
// httppost1.setEntity(entity);
HttpResponse response1 = httpclient1.execute(httppost1);
InputStream is = response1.getEntity().getContent();
HttpEntity entity1 = httppost1.getEntity();
Header[] headers = httppost1.getAllHeaders();
System.out.println(headers.length);
for(int i=0; i<headers.length;i++)
{
System.out.println(headers[i]);
}
String result = convertInputStreamToStringCommonIO(is);
System.out.println();
System.out.println(result);
int code1 = response1.getStatusLine().getStatusCode();
System.out.println("Code: "+code1+"");
String reason1 = response1.getStatusLine().getReasonPhrase();
System.out.println("Description: "+reason1 +"");
Temporary Request Headers from Postman:
UserAgent: PostmanRuntime/7.13.0
Accept: /
Cache-Contro: no-cache
Postman-Token: *****
Host: *****
accept-encoding: gzip,dflate
content-length:
Connection: keep-alive
Here's the error I am getting:
{"errorCode":"E0000021","errorSummary":"Bad request. Accept and/or Content-Type headers likely do not match supported values.","errorLink":"E0000021","errorId":"oae5BpTGp_5RjaFtGn_Zm_mhw","errorCauses":[]}
Code: 400
Description: Bad Request
Any idea why it's failing via code? Thanks.
This may be easy one, but I am confused.
I am trying to do HTTP POST on a server using Android Restlet, and read the reply returned from the server.
I created form using:
Form form = new Form
form.add("msg" ,"This is my message");
Now, I have clientResource as follows:
ClientResource clientResource = new ClientResource("www.example.com/my-http-post-url/");
Now, I am doing HTTP Post as:
Representation response=null;
try{
response= clientResource.post(form.getWebRepresentation(null));
System.out.println("Got response !! , response : " + response.getText());
System.out.println( "Got Context: " + clientResource.getContext() );
System.out.println( "Got Response: " + clientResource.getResponse());
System.out.println( "Got Resonse Attribute : " + clientResource.getResponseAttributes() );
System.out.println( "Got Resonse Entity: " + clientResource.getResponseEntity() );
}catch(Exception e){
e.printStackTrace();
}
I found out that the code is going inside try, but its printing :
I/org.restlet( 493): Starting the default HTTP client
I/System.out( 493): Got response !! , response : null
I/System.out( 493): Got Context: null
I/System.out( 493): Got Response: HTTP/1.1 - OK (200) - OK
I/System.out( 493): Got Resonse Attribute : {org.restlet.http.headers=[(Date,Sun, 22 Jul 2012 22:14:03 GMT), (Server,WSGIServer/0.1 Python/2.7.1+), (Vary,Authorization), (Content-Type,application/json; charset=utf-8)]}
I/System.out( 493): Got Resonse Entity: [application/json,UTF-8]
I tried sniffing the data, to see if the server is replying or not, I am confident that server is sending the response content.
Can anyone tell me, how can I find out the response content send by the server?
You're using client-side support of Restlet the right way. The response content should be contained within the response representation...
The first step is to call your REST service outside Android to see the exact response content. Can you try to do this using restclient (http://code.google.com/p/rest-client/) or curl?
Thierry
Try something like this:
I have something similar to this right now:
ClientResource clientResource = new ClientResource(url);
Request request = new Request(Method.POST, url);
clientResource.setRequest(request);
Form form = new Form();
form.set("foo", "barValue");
org.restlet.representation.Representation response = clientResource.post(form, MediaType.APPLICATION_JSON);
Representation responseEntity = clientResource.getResponseEntity();
JsonRepresentation jsonRepresentation = new JsonRepresentation(responseEntity);
JSONObject jsonObject = jsonRepresentation.getJsonObject();
String[] names = JSONObject.getNames(jsonObject);
if (jsonObject.has("errorString"))
{
String error = jsonObject.optString("errorString");
}