Sending request using json object return 404 error code

0 votes

I am getting mad with this code below.

When i send the request using the string directInput, it's working fine. Unfortunately when i used the json object converted into string jsonInput i got 404 error code. the directInput and jsonInput return the same values.

Someone could please help troubleshooting ? Thanks

public class Test {

public static void main(String[] args) {

try {

String userCredentials = "USR28:YG739G5XFVPYYV4ADJVW";

        String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));

        URL url = new URL("http://74.208.84.251:8221/QosicBridge/user/deposit");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");

conn.setRequestProperty("Content-Type", "application/json;");

conn.setRequestProperty("Accept", "application/json");

conn.setDoOutput(true);

conn.setRequestProperty ("Authorization", basicAuth);

//String directInput = "{\"msisdn\":\"22997858711\",\"amount\":1400,\"transref\":\"QOVNPVTRATYCBK8VIL1A\",\"clientid\":\"UBHQ\"}";

       

        DepositObject d = new DepositObject();

d.setMsisdn("22997858711");

d.setAmount(35);

d.setTransref("QOVNPVTRATYCBK8VIL1A");

d.setClientid("UHBQ");

Gson gson = new Gson();

String jsonInput = gson.toJson(d).toString();

OutputStream os = conn.getOutputStream();

os.write(jsonInput.getBytes());

os.flush();

if (conn.getResponseCode() != 200) {

throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());

}

BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream())));

String output;

while ((output = br.readLine()) != null) {

System.out.println(output);

}

conn.disconnect();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}
Jun 19, 2019 in Java by FooBayo
• 120 points

edited Jun 19, 2019 by FooBayo 2,930 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java

0 votes
1 answer

how to read csv file form sftp connection and store into string object in java code and convert into json.....post it using rest api

Hey, @Pooja, Before starting with anything you should ...READ MORE

answered May 13, 2020 in Java by Roshni
• 10,480 points
3,820 views
0 votes
1 answer

How can we return a JSON object from a Java Servlet?

response.setContentType("application/json"); // Get the printwriter object from response ...READ MORE

answered Jul 6, 2018 in Java by sharth
• 3,370 points
5,879 views
+1 vote
1 answer
0 votes
1 answer

Following error is occurring "Can't start Eclipse - Java was started but returned exit code=13"

There are combinations of Operating System, JDK ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
2,966 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,549 views
0 votes
1 answer

What is the simplest way to read JSON from a URL in java

Read json from url use url.openStream() and read contents ...READ MORE

answered Jun 13, 2018 in Java by samarth295
• 2,220 points
5,172 views
+1 vote
12 answers

Iterate over a JSONObject?

Assuming your JSON object is saved in ...READ MORE

answered Dec 11, 2020 in Java by Rajiv
• 8,870 points
124,722 views
0 votes
1 answer

Using Jackson to deserialise an array of object

First create a mapper : import com.fasterxml.jackson.databind.ObjectMapper;// in ...READ MORE

answered Sep 26, 2018 in Java by Sushmita
• 6,920 points
5,312 views
0 votes
1 answer

Escaping strings in JSON

You can find a JSON library in your ...READ MORE

answered Oct 4, 2018 in Java by geek.erkami
• 2,680 points
24,412 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP