How to send form-data key and values using patch request

2,809 views
Skip to first unread message

Sumit Bhardwaj

unread,
Sep 30, 2016, 4:06:41 PM9/30/16
to REST assured
I have a case where I need to send an form-data key and value in API for eg:

API: xyz.com

form-data key: test
form-data value: testValue

I already tried with body,formParam, parameters etc but always get a status code 301.

Please share some examples which showing sending form-data key value using patch request.

Thanks in advance.

Johan Haleby

unread,
Oct 1, 2016, 8:49:11 AM10/1/16
to rest-a...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sumit.b...@socialcodeinc.com

unread,
Oct 5, 2016, 7:04:39 AM10/5/16
to REST assured
Thanks John for your quick reply, can you please share some example showing how to send patch request having form-data key and values..

Thanks in advance..


On Saturday, October 1, 2016 at 6:19:11 PM UTC+5:30, Johan Haleby wrote:
On Fri, Sep 30, 2016 at 5:34 PM, Sumit Bhardwaj <17.sumit...@gmail.com> wrote:
I have a case where I need to send an form-data key and value in API for eg:

API: xyz.com

form-data key: test
form-data value: testValue

I already tried with body,formParam, parameters etc but always get a status code 301.

Please share some examples which showing sending form-data key value using patch request.

Thanks in advance.

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

Johan Haleby

unread,
Oct 5, 2016, 7:15:06 AM10/5/16
to rest-a...@googlegroups.com
Here's an example:

given().
formParam("firstName", "John").
formParam("lastName", "Doe").
when().
patch("/greetPatch").
then().
body("greeting", equalTo("Greetings John Doe"));
Which will send this HTTP request:

PATCH /greetPatch HTTP/1.1

Accept: */*

Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1

Content-Length: 27

Host: localhost:8080

Connection: Keep-Alive

User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_60)

Accept-Encoding: gzip,deflate


firstName=John&lastName=Doe



Regards,
/Johan

To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.

Sumit Bhardwaj

unread,
Oct 5, 2016, 8:08:05 AM10/5/16
to REST assured
Hi John,

I am using patch request in same way as you showed in an example, i always get a 301 status code, i think my app is not configured with spring security(not sure though), could you please help me how can is send patch request.. I also tried manually with postman it works perfectly fine with postman...please suggest how to move forward..

Thanks in advance..

Johan Haleby

unread,
Oct 5, 2016, 8:20:28 AM10/5/16
to rest-a...@googlegroups.com
See RFC 2616 which I alluded to in the blog:

   If the 301 status code is received in response to a request other
   than GET or HEAD, the user agent MUST NOT automatically redirect the
   request unless it can be confirmed by the user, since this might
   change the conditions under which the request was issued.
      
   Note: When automatically redirecting a POST request after
   receiving a 301 status code, some existing HTTP/1.0 user agents
   will erroneously change it into a GET request.


The default behaviour of HttpClient (which REST Assured is using) is compliant with the requirements of the HTTP specification (RFC 2616) and will thus NOT automatically follow this redirect. You should be able to change this by settings a custom HttpClient instance in REST Assured. Something like this:

DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new DefaultRedirectStrategy() {
    @Override
    protected boolean isRedirectable(String method) {
        return true;
    }
});

Regards,
/Johan

To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.

Johan Haleby

unread,
Oct 5, 2016, 8:29:03 AM10/5/16
to rest-a...@googlegroups.com
I've added an issue to make configuring this easier in the future.

Sumit Bhardwaj

unread,
Oct 5, 2016, 8:33:18 AM10/5/16
to REST assured
Thanks alot John for your help (Y)...

ASHWIN KARANGUTKAR

unread,
Nov 4, 2016, 1:50:40 AM11/4/16
to REST assured
Hi Sumit,
Below is an example of automating PATCH Request using Rest Assured:

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static com.jayway.restassured.RestAssured.*;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;

public class PatchRequestTest {

  @BeforeClass
  public void setBaseUri () {

    RestAssured.baseURI = "https://localhost:3000";
  }


@Test
  public void updateUsingPatch () {
    
    Posts post = new Posts();
    post.setId ("3");
    post.setTitle ("Hello Vietnam");
    
    given().body (post)
    .when ()
    .contentType (ContentType.JSON)
    .patch ("/posts/3");

}
}

You can read a detailed tutorial on PATCH Request using Rest Assured at the the following link:

Thanks,
Ashwin Karangutkar

Reply all
Reply to author
Forward
0 new messages