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 in Java by FooBayo
• 120 points

edited Jun 19 by FooBayo 41 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 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,320 points
1,570 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,540 points
779 views
+14 votes
18 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 in Java by Suresh
• 700 points
77,897 views
0 votes
2 answers

I'm getting following error, while i'm running code in windows xp service pack 3 with JDK 6 version.

Till you send the code, I would ...READ MORE

answered May 16, 2018 in Java by Meci Matt
• 9,420 points
213 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,190 points
222 views
+1 vote
10 answers

Iterate over a JSONObject?

You can use the following method to ...READ MORE

answered Dec 7, 2018 in Java by Shuvodip
14,139 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,880 points
273 views
0 votes
1 answer

Escaping strings in JSON

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

answered Oct 3, 2018 in Java by geek.erkami
• 2,320 points
525 views