2

I am using processing to do a simple POST request on a server. I have tried with many accept header configurations and I keep getting Error - 406 Not Acceptable. However, when I do requests with postman (chrome plugin) I get no errors.

I get errors with this code, which I irresponsibly tweaked from Java - sending HTTP parameters via POST method easily

URL url;
  try {
    url = new URL( urlString.toString() );
  }
  catch(MalformedURLException e) {
    e.printStackTrace();
    url = null;
  }
  Map <String, String> params = new HashMap<String, String>();
  params.put("email", email );
  params.put("mac", macAddress );

  StringBuilder postData = new StringBuilder();
  for ( String s : params.keySet() ) {
    if (postData.length() != 0) postData.append('&');
    postData.append(encode( s ));
    postData.append('=');
    postData.append( encode( params.get(s) ) );
  }
  byte[] postDataBytes;

  try { 
    postDataBytes = postData.toString().getBytes("UTF-8");
  }
  catch(UnsupportedEncodingException e) {
    e.printStackTrace();
    postDataBytes = null;
  }

  HttpURLConnection conn;

  try { 
    conn = (HttpURLConnection)url.openConnection();
  }
  catch(IOException e) { 
    e.printStackTrace();
    conn = null;
  }
  try {
    conn.setRequestMethod("POST");
  }
  catch(ProtocolException e) {
    e.printStackTrace();
  }
  conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
  conn.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
  //conn.setRequestProperty("Accept-Charset", "UTF-8");

  conn.setRequestProperty("Accept-Language", "es-ES,es;q=0.8,en;q=0.6");
  conn.setRequestProperty("Accept", "*/*");


  conn.setDoOutput(true);

  try {
    conn.getOutputStream().write(postDataBytes);
  } 
  catch(IOException e) {
    e.printStackTrace();
  }

  InputStreamReader isr;
  try {
    isr = new InputStreamReader( conn.getInputStream() );
  }
  catch(IOException e) {
    e.printStackTrace();
    isr = null;
  } 
  BufferedReader in = new BufferedReader( isr );


  try {
    licencia = in.readLine();
  }
  catch(IOException e) {
    e.printStackTrace();
  }

When i do the request by postman (chrome plugin), everything is fine. I monitored the request using http://requestb.in/:

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8,en;q=0.6
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
Content-Length: 40
Content-Type: application/x-www-form-urlencoded
Host: requestb.in
Cookie: session=eyJyZWNlbnQiOlsid3N0MnVkd3MiXX0.BmRxhg.8OCooRI-dXug4izYc_-96Gqxa54
Cache-Control: no-cache
X-Request-Id: 9cad2bbe-84d1-496c-8d3d-26e4f2b8455f
Connection: close

Raw body:

email=sergio%40urlEncoded&mac=123POSTMAN

So I did my best use similar accept-headers:

User-Agent: Java/1.6.0_33
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8,en;q=0.6
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
Host: requestb.in
X-Request-Id: aa9a9739-3fc9-4cc7-a946-afaad04d178c
Connection: close

Raw body:

email=%40.&mac=C8BCC8DF14A8

But I still get the 406 error. ¿What may be the problem? ¿Is this a server-side thing?

Sergio

Community
  • 1
  • 1
Sergio
  • 377
  • 1
  • 3
  • 18

0 Answers0