5

I want to send an HTTP Post request using 'form-data'. Here is a screenshot of the rest-client to show what do i want:

enter image description here

More Information related to headers:

POST /action HTTP/1.1
Host: requsrt-utl
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="email"

abc@gmail.com
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="password"

123456
----WebKitFormBoundaryE19zNvXGzXaLvS5C

I tried using UrlEncodedFormEntity but it is setting the content-type as 'application/x-www-form-urlencoded' which is not correct.

My Android Code:

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(urlStr);
        try {
            UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(nameValuePairs);
            httppost.setEntity(encodedFormEntity);
            HttpResponse httpresponse = httpclient.execute(httppost);
            return httpresponse.getEntity().getContent();

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

When i am using this, web-service is not getting any parameter and hence it sending a message that 'parameters are absent'.

Please help!

mudit
  • 25,306
  • 32
  • 90
  • 132

1 Answers1

5

This may help you

HttpPost httppost = new HttpPost(urlStr);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  

nameValuePairs.add(new BasicNameValuePair("userid", "12312"));  
nameValuePairs.add(new BasicNameValuePair("sessionid", "234"));  

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

how to add parameters in android http post?

Community
  • 1
  • 1
BlaShadow
  • 11,075
  • 7
  • 39
  • 60
  • I am doing same but always getting '415 unsupported media' type. I am not even setting any content-type header. – Darpan Jun 10 '15 at 08:57
  • you may need to set the header 'Content-Type'. `request.addHeader("Content-Type","application/x-www-form-urlencoded");` – BlaShadow Jun 10 '15 at 11:29
  • Content header was not needed. They created a multi-part http call, So I made that. – Darpan Jun 10 '15 at 11:51