why does the program need to check API header "Accept" - java

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();
}

Related

HttpRequest.BodyPublishers.ofString(jsonasstring) does not put anything in the POST request

I'm trying to make a POST request with custom headers and json as string on body
Here's my code
HttpRequest request2 = HttpRequest.newBuilder()
.uri(URI.create(POSTS_API_URL))
.headers("accept", "text/plain; charset=UTF-8", "XF-Api-Key", "MYAPIKEY")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
System.out.println(request2); //result : https://******.fr/api/auth/ POST
System.out.println(request2.headers()); //result : java.net.http.HttpHeaders#8e33ff08 { {accept=[text/plain; charset=UTF-8], XF-Api-Key=[MYAPIKEY]} }
HttpResponse<String> response2 = client.send(request2, HttpResponse.BodyHandlers.ofString());
// print status code
System.out.println(response2.statusCode()); //400
// print json code
System.out.println(json); //{"login":"LunaLune","password":"***********"}
// print response body
System.out.println(response2.body()); //mandatory input missing : login, password
And my json String
String json = "{" +
"\"login\":\"LunaLune\"," +
"\"password\":\"*********\"" +
"}";
But when I print the request I get : https://*******.fr/api/auth/ POST
the POST request is empty
I googled many forums, code examples ect... but I see that my code where correct according many examples I seen.
So if someone know what is my problem ?
Thanks in advance !
You need to set "Content-Type" as "application/json" in the request header.
See: Which JSON content type do I use?

Why is HttpServletResponse body always empty?

My client is using the fetch api to interact with a Controller method that will decrypt a password and send the decrypted password back to the client as plain text. However, the response body never contains the password. It also continues to set the content type as basic even though I am setting it to text/plain. What am I doing wrong?
Client
function showCredentialModal(credentialId, url, username, password) {
$('#credential-id').val(credentialId ? credentialId : '');
$('#credential-url').val(url ? url : '');
$('#credential-username').val(username ? username : '');
// Display encrypted password if the current user is not the owner of the credential
if (password) {
fetch('/credentials/decrypt?credentialId=' + credentialId)
.then(response =>
console.log(response))
.catch(() =>
$('#credential-password').val('error'));
} else {
$('#credential-password').val('');
}
$('#credentialModal').modal('show');
}
Controller
#GetMapping("/decrypt")
public void doGet(HttpServletResponse response,Authentication authentication,
#ModelAttribute Credential credential) throws IOException {
User user = getCurrentUser(authentication);
credential = credentialService.getCredential(credential.getCredentialId());
boolean result = validationService.validate(credential, user);
if (result) {
String decryptedPassword = credentialService.decryptPassword(credential);
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
try (PrintWriter out = response.getWriter()) {
out.print(decryptedPassword);
out.flush();
}
}
}
Response:
Response {type: "basic", url: "http://localhost:8080/credentials/decrypt?credentialId=1", redirected: false, status: 200, ok: true, …}
body: ReadableStream
locked: false
__proto__: ReadableStream
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: ""
type: "basic"
url: "http://localhost:8080/credentials/decrypt?credentialId=1"
__proto__: Response
Your client just prints the response to console:
.then(response => console.log(response))
which shows a response with a ReadableStream body that isn't consumed:
body: ReadableStream
...
bodyUsed: false
You need to read the content of that stream to get the content the servlet returned. See for example:
Using readable streams
Body mixin
Right now you are just dumping the Response to console and its string representation isn't what you expect it to be (i.e. it's not the content but a wrapper for it). Your servlet code seems fine, it's your JavaScript client needs to be modified to read the content from within the response.
Try to debug. Actually what is wrote over very difficult to understand. Here could be some cases, but sure that "result" returns always false.
Some question for debugging:
Could method getCurrentUser() be consumed with null?
credentialId; It consumed from URL parameters that you pass in fetch method.
My suggestion to rewrite this code using samples in Spring Documentation.
Now it looks like you copied some snippets from different guides.

Send the API Headers in Rest Assured using java

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 :)

Python requests POST to java REST interface MultipartFile parameter is not present

I've searched around here as well as elsewhere online and can't seem to find the answer for what I think is a simple error on my part. Basically I want to transfer a file from one machine to another by issuing a Python requests.POST request to a Java REST interface on the remote machine. The Java side looks like this:
#ApiOperation(value = "Binary file transfer", nickname = "Binary file transfer")
#ApiResponses(value = {
#ApiResponse(code = 200, message = "Success", response = HttpMessageInformationReturnDataBean.class),
#ApiResponse(code = 404, message = "Not Found")})
#RequestMapping(value = "/vm/{version}/uploadbinfile", method = RequestMethod.POST)
public String handleFileUpload(#RequestParam("binaryFile") MultipartFile file) {
if (!file.isEmpty())
{
try
{ ... the code that handles the transfer
On the Python side, the method looks like this:
def xfer_trm_binaries(self):
params = {"file": ('binaryFile',os.path.basename('TRMServer.jar')),
"folder": os.path.dirname(self.dest_temp_path),
"submit": "Submit"}
url = self.form_url("/vm/v1/uploadbinfile", self.trm_server_ip_address, self.vrm_server_port)
header=self.form_header(self.vrm_key)
header['Content-Type'] = 'multipart/file-data; boundary=randomboundarysequence'
header['enctype'] = "multipart/file-data"
print 'Send :' + url
binfile = self.local_jar_path+'TRMServer.jar'
with open(binfile, 'rb') as mfile:
try:
result = requests.post(url, headers=header,
data=params, files={'file': mfile}, verify=False)
except Exception:
The header that gets assembled there looks like this:
{'Content-Type': 'multipart/file-data; boundary=randomboundarysequence', 'Accept': 'application/json', 'Authorization': u'Bearer 8b2b6e53-9008-44b7-9d34-b5ecb9659250', 'enctype': 'multipart/file-data'}
The request is sent, however the response is always a 400 error, because it complains the MultipartFile parameter 'binaryFile' is missing:
'{"timestamp":1488597880207,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required MultipartFile parameter \\'binaryFile\\' is not present","path":"/vm/v1/uploadbinfile"}'
I've tried adding a 'name' value to both the params and headers of the request but it always comes back with the 400 code. Does anyone out there know what I might be doing wrong?
Actually I eventually figured this out - basically I had a method that formed the header to include the oauth bearer token, along with the ContentType and AcceptType...I then overwrote those with the multipart file info. THAT was what the receiving REST interface didn't like. When I just eliminated those header attributes altogether, it seemed to figure it out on its own.

HTTP PUT to upload a file in Java

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.

Categories