I've tried in postman successfully,by adding Header"Content-Type=application/x-www-form-urlencoded" with x-www-form-urlencoded body.Apparently, this was my first time working on rest. Please advise me that below my code is correct or not. Thank You.
RestAssured.baseURI = AssignConfig.app.getProperty("RestURL");
Response request = RestAssured
.given()
.config(RestAssured.config()
.encoderConfig(EncoderConfig.encoderConfig()
.encodeContentTypeAs("x-www-form-urlencoded", ContentType.URLENC)))
.contentType("application/x-www-form-urlencoded; charset=UTF-8")
.header("Content-Type", "application/x-www-form-urlencoded")
.formParam("login","userName")
.formParam("password","password")
Related
I have a nodejs web server running and I want to a body to the request in my java program.
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.header( "Content-Type", "text/plain;charset=UTF-8")
.uri(URI.create("http://localhost:3000"))
.POST(HttpRequest.BodyPublishers.ofString("Hello"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("status:" + response.statusCode());
System.out.println("response:" + response.body());
If I delete this line: .POST(HttpRequest.BodyPublishers.ofString("Hello")) everything works fine, I get the response from the server, but there is no body to the request.
With this line I get status code 404 and the body of the response to the client is:
How can I add body to the request?
First of all try understand differences between GET and POST HTTP methods
https://www.w3schools.com/tags/ref_httpmethods.asp
There is also a good example in docs: https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpRequest.BodyPublishers.html
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com/"))
.header("Content-Type", "text/plain; charset=UTF-8")
.POST(BodyPublishers.ofString("some body text"))
.build();
You can also look here as well
https://www.baeldung.com/java-9-http-client
What is the difference with UniRest and Spring RestTemplate which is giving back a 400 Bad Request with apparently the same header and body sent ?
I try to reach the HubSpot API to create a BlogPost, but using RestTemplate I have a 400 Bad Request error, and using UniRest works alright (returns an OK response). However, I do not want to include a library just to make one REST call: I'd rather stick to RestTemplate.
The request data I need to send
HttpMethod: POST
URL: https://api.hubapi.com/content/api/v2/blog-posts?hapikey=*****************
Header: Content-Type: application/json
Body: (represented by a class instance as blogpostSendPost further down)
{
"name": "My first API blog post!",
"content_group_id": 351076997
}
Using RestTemplate
Setting up the request:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<BlogpostSendPost> request = new HttpEntity<>(blogpostSendPost, headers);
log(request.toString());
//LOG PRINT: <BlogpostSendPost(name=My first API blog post!, content_group_id=351076997),[Content-Type:"application/json"]>
OR in JSON
The .json() method converts my object in Json like you can see in the logs
HttpEntity<String> request = new HttpEntity<>(blogpostSendPost.toJson(), headers);
log(request.toString());
//LOG PRINT: <{"name":"My first API blog post!","content_group_id":"351076997"},[Content-Type:"application/json"]>
With .postForObject(): 400 Bad Request
BlogpostResponsePost answer = restTemplate.postForObject(
"https://api.hubapi.com/content/api/v2/blog-posts?hapikey=***********",
request,
BlogpostResponsePost.class);
With .exchange(): 400 Bad Request
BlogpostResponsePost answer = restTemplate.exchange(
"https://api.hubapi.com/content/api/v2/blog-posts?hapikey=**********",
HttpMethod.POST,
request,
BlogpostResponsePost.class);
Using UniRest: OK
HttpResponse<JsonNode> resp = Unirest
.post("https://api.hubapi.com/content/api/v2/blog-posts?hapikey=**********")
.header("Content-Type", "application/json")
.body(blogpostSendPost)
.asJson();
I am using PostMan to call my REST SpringBoot Application which is using theses Services : when I am calling the HubSpot API directly from PostMan it works fine, just like with UniRest lib.
Thanks for your help guys !!
Please refer https://community.hubspot.com/t5/APIs-Integrations/Getting-400-Bad-Request-when-trying-to-add-a-Blog-Post/td-p/306532
Instead of converting request object to json, pass request object directly. It worked for me.
// TRY 1: CONTACTS - RestTemplate - OK - contact is created (API V1)
HttpEntity request1 = new HttpEntity<>(contactSendList, headers);
ContactResponseInformations answer1 = restTemplate
.postForObject(
HubSpotConfiguration.URL_CREATE_CONTACT,
request1,
ContactResponseInformations.class);
log.info(answer1.toString()); // OK
I am trying to access Mindsphere URL with Java Code. I am getting 403 forbidden error while doing it. While I am able to hit other POST URL's for other sites, Mindsphere URL is getting blocked by same piece of Java Code. Can someone help?
What am i missing in my Code?
restTemplate.exchange(,,*,TimeseriesData.class) is line giving error
MindSphere demands a authorization header with a JWT Token, if you call directly the API. I guess you have an Developer account in MindSphere. Try Application credentials in the Developer cockpit. With that credentials you can get a bearer token with an oauth flow.
If not just ping me again.
See Exampel with OK HTTP
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials");
Request request = new Request.Builder()
.url("https://questdev.piam.eu1.mindsphere.io/oauth/token")
.post(body)
.addHeader("Accept", "application/json")
.addHeader("cache-control", "no-cache,no-cache")
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Postman-Token", "24126d6b-3461-48fb-9060-6fd005804227")
.build();
Response response = client.newCall(request).execute();
I'm new to Java and have been looking at a couple of codes for Fluent Request. Most of it made sense except for a tiny bit. In the following code, can I know the difference between .addHeader("content-type", "application/json") and .bodyString(json, ContentType.APPLICATION_JSON), please. Aren't they both specifying that the content type should be Json?
httpResponse = Request.Post(URL)
.addHeader("content-type", "application/json")
.addHeader("Accept", "application/json")
.bodyString(json, ContentType.APPLICATION_JSON)
.execute()
.returnResponse();
My guess is that setting ContentType in bodystring() adds the "content-type" header itself.
So you would use addHeader("content-type", "application/json") explicitly if you need to send a request that does not contain any body data, otherwise just use bodystring().
I am trying to make a POST with javax. Keep getting the 400 bad request response code. It suggests that there is something wrong with the json being sent(?) I have checked the json a hundred times, it looks pretty good to me. What am I missing?
enter code here
Client client = ClientBuilder.newClient();
Entity payload = Entity.json("{ 'offset': 0, 'limit': 15, 'query': 'ad', 'search_type': 'global'}");
Response response = client.target("https:******/contacts/search")
.request(MediaType.APPLICATION_JSON_TYPE)
.header("Accept", "application/json")
.header("authToken", ACCESS_TOKEN)
.post(payload);