How to pass post json Object Data to an api using java/Spring/rest

12,500

Solution 1

You can using okhttp (https://github.com/square/okhttp) to call this api. Example:

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\t\"name\":\"hotel california\", \n\t\"createdAt\":1505727060471, \n\t\"steamUrl\":\"https://www.youtube.com/watch?v=lHje9w7Ev4U\"\n}");
Request request = new Request.Builder()
  .url("http://eventapi-dev.wynk.in/tv/events/v1/event")
  .post(body)
  .addHeader("content-type", "application/json")
  .addHeader("cache-control", "no-cache")
  .addHeader("postman-token", "08af0720-79cc-ff3d-2a7d-f208202e5ec0")
  .build();

Response response = client.newCall(request).execute();

Solution 2

You have to use something similar as described here maily bu using HttpURLConnection & URL .

There you notice for post scenario that JSON data is passed as String

Then you can follow this question also to know few more answers using that API.

You can also use Apache HttpClient and browse examples on their site.

Apache HttpClient examples are here too.

I am not sure if I should just copy - paste relevant code samples from those websites to this answer ( for the sake of completeness ) but idea is very simply that you have to find an API that helps you in building and executing a REST request.

Another API is listed in answer by - Trần Đức Hùng and so we have numerous other Java APIs available in the market.

Share:
12,500
Jalaj Chawla
Author by

Jalaj Chawla

I am a Backend Developer with TechStack of Java and its framework (Spring,Hibernate,WebServices and Hadoop)

Updated on June 26, 2022

Comments

  • Jalaj Chawla
    Jalaj Chawla almost 2 years

    Problem Statement

    I am new to Spring/Rest Applications and I have data in an Object.

    Now,I need to pass this data to an API.

    Below is the sample curl Attached for a single record-

    curl --request POST \
      --url http://eventapi-dev.wynk.in/tv/events/v1/event \
      --header 'cache-control: no-cache' \
      --header 'content-type: application/json' \
      --header 'postman-token: 67f73c14-791f-62fe-2b5a-179ba04f67ba' \
      --data '{"name":"hotel california", "createdAt":1505727060471, "steamUrl":"https://www.youtube.com/watch?v=lHje9w7Ev4U"}'
    

    The response I got after Hitting curl url in Terminal is Ok

    Can I anyone guide me how to write the Code in Java.

  • Jalaj Chawla
    Jalaj Chawla over 6 years
    @Tran Duc Hung How to pass my object to this.
  • Sahin Yanlık
    Sahin Yanlık over 6 years
    lol this is totally different :)) anyway maybe someone try to replicate end-point.
  • Sabir Khan
    Sabir Khan over 6 years
    @JalajChawla: I guess answer is pretty clear that you need to pass your JSON on line - RequestBody body = RequestBody.create(..., ...) or your question something else ? Alternately, you can use a JSON API like Jackson etc to represent your POJOS as JSON objects and then convert to strings etc.
  • Trần Đức Hùng
    Trần Đức Hùng over 6 years
    @JalajChawla u can use gson