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"

[email protected]
----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!

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

I am doing same but always getting '415 unsupported media' type. I am not even setting any content-type header.
you may need to set the header 'Content-Type'. request.addHeader("Content-Type","application/x-www-form-urlencoded");
Content header was not needed. They created a multi-part http call, So I made that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.