I using java Unirest to call my api. but now I need to post an object but with x-www-form-urlencoded, I using this code:
public static void main(String[] args) {
Unirest.config().setObjectMapper(new JacksonObjectMapper());
System.out.println(Unirest
.post("http://192.168.2.157:8082/auth/realms/collatum/protocol/openid-connect/token")
.header("Content-Type", "application/x-www-form-urlencoded")
.body(RequestToken.builder()
.username("user")
.password("1234")
.grant_type("password")
.client_id("front")
.build()).asString().getBody()
);
}
and I get this error:
{"error":"invalid_request","error_description":"Missing form parameter: grant_type"}
When I analise the request the header are ok:(but looks like the object are in json and no x-www-form-urlencoded
POST /auth/realms/collatum/protocol/openid-connect/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
user-agent: unirest-java/3.1.00
accept-encoding: gzip
Content-Length: 87
Host: 192.168.2.157:8082
Connection: Keep-Alive
{"grant_type":"password","client_id":"front","username":"user","password":"1234"}HTTP/1.1 400 Bad Request
Cache-Control: no-store
X-XSS-Protection: 1; mode=block
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
Referrer-Policy: no-referrer
Date: Thu, 20 Jan 2022 13:56:53 GMT
Connection: keep-alive
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Content-Type: application/json
Content-Length: 84
{"error":"invalid_request","error_description":"Missing form parameter: grant_type"}
It looks like your body is in the wrong form for application/x-www-form-urlencoded.
It shouldn't be a json object and instead should be something like grant_type=password&client_id=front&username=user&password=1234
An example can be found here
Related
I read a lot issues with that here, but it's not working for me. May be I doing something wrong?
my retrofit Interface:
#Multipart
#POST("/api/v1/services/add")
suspend fun addServiceAsync(#Part("newService") service: ServiceApiData,
#Part imageFile:MultipartBody.Part?,
#Header("Authorization") jwt:String?): StatusAndStrApi?
And Spring RestController part is
#PostMapping("/add")
ResponseEntity<?> addService(#RequestPart(name = "newService") ServiceServer service,
#RequestPart("imageFileFromClient") MultipartFile imageFile
)
Here how i create Multipatr.Body:
MultipartBody.Part.createFormData(
"imageFileFromClient",
fileImage.name,
fileImage.asRequestBody("image/*".toMediaTypeOrNull()
)
Here what server return me:
I/okhttp.OkHttpClient: <-- 400 http://server:8080/api/v1/services/add (301ms)
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
I/okhttp.OkHttpClient: Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/json
I/okhttp.OkHttpClient: Transfer-Encoding: chunked
Date: Fri, 03 Sep 2021 05:11:18 GMT
Connection: close
{"timestamp":"2021-09-03T05:11:18.689+00:00","status":400,"error":"Bad Request","path":"/api/v1/services/add"}
I/okhttp.OkHttpClient: <-- END HTTP (110-byte body)
I/System.out: close [socket][/100.107.22.171:34172]
Try using https instead of http.
BODY WAS NULL, but it mustn't be. Don't allow Multipartbody be nullable. Never.
I have a strange problem in WildFly 12. When I post a HTTP request sometimes body is replaced by HTTP response header. Sometimes it doesn't.
the error json data:
HTTP/1.1 200 OK^M Expires: 0^M Cache-Control: no-cache, no-store, max-age=0, must-revalidate^M X-Powered-By: Undertow/1^M Server: WildFly/11^M X-XSS-Protection: 1; mode=block^M Pragma: no-cache^M X-Frame-Options: SAMEORIGIN^M Date: Thu, 08 Nov 2018 13:00:25 GMT^M Connection: keep-alive^M X-Content-Type-Options: nosniff^M Content-Type: text/plain;charset=UTF-8^M Content-Length: 1569^M ^M ationId":"61e1e1b0867c11e8a9a81fbc702bb402","value":"MU5123","startStationName":"上海虹桥国际机场","endAreaName":"北京首都国际机场"},{"shiftId":"6c874935c59e11e8a4f557d52226d05c","startAreaId":"61e1e1b0867c11e8a9a81fbc702bb402","endStationName":"北京首都国际机场","lineId":"336fb55d-cc7c-11e8-9ed0-00163e02896a","label":"MU5159","dispatchTime":"14:30","endAreaId":"61e1e193867c11e8a9a81fbc702bb402","endStationId":"61e1e193867c11e8a9a81fbc702bb402","startAreaName":"上海虹桥国际机场","startStationId":"61e1e1b0867c11e8a9a81fbc702bb402","value":"MU5159","startStationName":"上海虹桥国际机场","endAreaName":"北京首都国际机场"},{"shiftId":"6c874930c59e11e8a4f557d52226d05c","startAreaId":"61e1e1b0867c11e8a9a81fbc702bb402","endStationName":"北京首都国际机场","lineId":"9db7b2a5c5a911e89ed000163e02896a","label":"MU5331","dispatchTime":"15:00","endAreaId":"61e1e193867c11e8a9a81fbc702bb402","endStationId":"61e1e193867c11e8a9a81fbc702bb402","startAreaName":"上海虹桥国际机场","startStationId":"61e1e1b0867c11e8a9a81fbc702bb402","value":"MU5331","startStationName":"上海虹桥国际机场","endAreaName":"北京首都国际机场"}]
Could someone let me know the reason would be for this behavior?
I'm trying to use JMeter to test a restful endpoint for uploading files, but I'm getting a 400 error. The one thing that jumps out at me is the boundary value; it's not the same I provided as the one shown in the request. I'm able to use the endpoint in the browser without issue, and I've replicated the headers revealed in the developer tools in FF.
Here is the relevant info from JMeter:
Result Tab:
Thread Name: Asdf - Load Test 1-1
Sample Start: 2017-06-05 08:47:46 EDT
Load time: 159
Connect Time: 28
Latency: 159
Size in bytes: 438
Sent bytes:821003
Headers size in bytes: 438
Body size in bytes: 0
Sample Count: 1
Error Count: 1
Data type ("text"|"bin"|""):
Response code: 400
Response message: Bad Request
Response headers:
HTTP/1.1 400 Bad Request
Date: Mon, 05 Jun 2017 12:47:46 GMT
Server: Apache/2.4.25 (Win64) OpenSSL/1.0.2k
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Cache-Control: no-cache, must-revalidate
Content-Length: 0
Connection: close
HTTPSampleResult fields:
ContentType:
DataEncoding: null
Request Tab:
POST https://localhost/my/rest/endpoint
POST data:
--9amm365-gMmimP70lvs9jIvlIxOfkocUN
Content-Disposition: form-data; name="parseable"; filename="asdf.docx"
Content-Type: application/vnd.openxmlformats-
officedocument.wordprocessingml.document
Content-Transfer-Encoding: binary
--9amm365-gMmimP70lvs9jIvlIxOfkocUN--
[no cookies]
Request Headers:
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--Uc_2uLvcVgc7SqvzIJxR3encUKw-
f7w9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.5
Accept: application/json, text/plain, /
X-Request-OnBehalfOf: some-user-uuid
X-Requested-With: XMLHttpRequest
Content-Length: 820532
Host: localhost
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_121)
Response Data Tab is blank.
Server side implementation:
#POST
#Override
#EnhancedDetail
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Path("my/rest/endpoint")
public Response uploadProduct(#PathParam("id") final String id, MultiPart multipart) throws IOException {
return processMultiFileUpload(id, multipart, MultiPartUploadType.DRAFT, false);
}
If you are building your request manually like it is described in Testing REST API File Uploads in JMeter article - make sure you have Use multipart/form-data for POST box of the HTTP Request sampler is not checked.
And vice versa, if you tick this box, you won't need to override the Content-Type header
I believe the easiest way would be just recording your file upload event using HTTP(S) Test Script Recorder (just make sure your asdf.docx file is copied to the "bin" folder of your JMeter installation)
I figured it out. I changed the Client Implementation on the HTTP Request to Java. Everything I've read said use HttpClient4, but I tried Java and it worked.
The REST service I want to consume answers as a gzipped encoded JSON. It provides the Content-Encoding: gzip, but my OkHttp does not encode it to readable text, so the JSON converter throws an exception.
---> HTTP GET https://rapla.dhbw-karlsruhe.de/rapla/events?resources=%5B%27rc85dbd6-7d98-4eb7-a7f6-b867213c73d8%27%5D&start=2015-09-01&end=2015-12-31
Accept-Encoding: gzip, deflate
Accept: application/json
Authorization: *not posted*
Content-Type: application/json;charset=utf-8
---> END HTTP (no body)
<--- HTTP 200 https://rapla.dhbw-karlsruhe.de/rapla/events?resources=%5B%27rc85dbd6-7d98-4eb7-a7f6-b867213c73d8%27%5D&start=2015-09-01&end=2015-12-31 (13ms)
Date: Tue, 24 Nov 2015 09:09:10 GMT
Server: Jetty(9.2.2.v20140723)
Expires: Tue, 01 Jan 1980 00:00:00 GMT
Pragma: no-cache
Cache-Control: no-cache, must-revalidate
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Content-Disposition: attachment
Content-Length: 9684
Via: 1.1 rapla.dhbw-karlsruhe.de
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1448356149978
OkHttp-Received-Millis: 1448356149991
����WK�{��J�`k�_��Z����E�p�>3m�WMa�ג�ҵ�p�0��<��
... skipped rest of the body
E��>���S���n
<--- END HTTP (9684-byte body)
According to Jake Whartons comment the Content-Encoding: gzip Header should tell OkHttp to decode the body.
The code for creating the RestAdapter is:
final RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(baseUrl)
.setClient(new OkClient(new OkHttpClient()))
.setConverter(new GsonConverter(gson))
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
service = adapter.create(RaplaService.class);
The gradle dependencies are:
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp:2.6.0'
The method in my ServiceInterface:
#Headers({
"Accept-Encoding: gzip, deflate",
"Content-Type: application/json;charset=utf-8",
"Accept: application/json"
})
#GET("/events")
List<Event> getEvents(#Header("Authorization") String token, #Query("resources") String resources, #Query("start") String start, #Query("end") String end);
Replace this:
#Headers({
"Accept-Encoding: gzip, deflate",
"Content-Type: application/json;charset=utf-8",
"Accept: application/json"
})
With this:
#Headers({
"Content-Type: application/json;charset=utf-8",
"Accept: application/json"
})
When you provide your own Accept-Encoding header you’re instructing OkHttp that you want to do your own decompression. By omitting it, OkHttp will take care of both adding the header and the decompression.
In My case I commented client(OkHttpClient client) following is the snippet code
Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BuildConfig.END_POINT)
//.client(client)
.build()
Because I am using LoggingInterceptor i think he will not take care everything related to gzip
I'm using java sockets to communicate with my server. I'm getting http response from the server in a byte[] format. Now I need to parse the headers and content of the byte[]. The sample response I'm getting from server is :
HTTP/1.1 200 OK
alternate-protocol: 443:quic,p=0.01
cache-control: no-cache, must-revalidate
content-disposition: attachment; filename="f.txt"
content-encoding: gzip
content-length: 341
content-type: text/javascript; charset=UTF-8
date: Sat, 15 Nov 2014 18:32:55 GMT
expires: -1
pragma: no-cache
server: gws
status: 200 OK
version: HTTP/1.1
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
1. Either I need an easiest way to parse this in Java (May be a library)
2. Or a way to convert it into HttpResponse object.
Apache HttpComponents is your friend in this case.
Please take a look at http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpParser.html