87

I am trying to make a PATCH call to a server. I am using the following command:

curl --data status=closed -X PATCH https://api.viafoura.com/v2/dev.viafoura.com/pages/7000000043515 

Is this the correct way of sending the PATCH request? I am getting an error saying there is no status parameter set. I am guessing the --data is for POST request only and thus the server doesn't find the status parameter.

This is the response (FYI):

{"http_status":400,"error":"Parameter validation errors","validation_errors":{"status":{"error":"Request missing status parameter."}}}

You can find documentation about this service here.

sheidaei
  • 9,842
  • 20
  • 63
  • 86

6 Answers6

123

This is the format you should use:

curl --request PATCH https://api.viafoura.com/v2/dev.viafoura.com/pages/7000000043515?status=closed

That API seems to want the status parameter as a query parameter on the url, not part of the PATCH body.

At this point the server is going to return a 401 error: "You must be logged in to modify page settings." Assumedly you have to login first with something like this:

curl --request POST "https://api.viafoura.com/v2/dev.viafoura.com/users/login?password=TeNn!sNum8er1&email=novak@example.com"

I've used the credentials from their documentation in that example, which I figured would work on their dev server, but its currently returning an "Incorrect password" error.

If you have valid credentials, though, you should get back a session cookie which you can then use to authenticate your PATCH request.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
James Holderness
  • 22,721
  • 2
  • 40
  • 52
21

I was trying to PATCH to a tastypie resource with a similar curl request. For me the problem was the data had to be passed in like so:

curl --data '{"field": "new_value"}' -X PATCH http://127.0.0.1:8000/api/v1/resource_uri/pk/

Notice how what I pass to the data flag is inside of what looks like a dictionary passed as a string, rather than putting the param directly as in the question. Of course a param works too as already answered, but hopefully this helps some people.

Nick Brady
  • 6,084
  • 1
  • 46
  • 71
  • 5
    Sometimes it is required to pass json header if the body in --data is json: `--data '{"name": "new name"}' -H "content-type: application/json"` ! – Vladimir Vukanac Apr 06 '20 at 12:17
5

For those who run it on Windows, with a complex patch expression for more than one property.
The following worked for me:

curl -X PATCH "http://localhost:5001/tenants/test02" -H "Content-Type: application/json" -d [{"""op""":"""replace""","""value""":"""100""","""path""":"""/employmentEndSettings/daysLoginActive"""},{"""op""":"""replace""","""value""":"""retiree""","""path""":"""/employmentEndSettings/userRoleAfter"""}]
d_f
  • 4,599
  • 2
  • 23
  • 34
2

Your command line should work. As you can see in the PATCH RFC5789, the HTTP request is similar what curl sends (use --trace-ascii to get to see the full curl communication). You might want to change the Content-Type (using --header).

The mentioned missing status parameter is probably referring to contents in the request-body. Your "status=closed" data is possibly not in the right format (JSON?) or it is incomplete.

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
2

This is something which worked for me in my sample app.

curl --data 'id=57&equipment_type_name=57 edited' -X PATCH http://localhost:5009/equipment-type/update
{
  "info": "Equipment type updation.",
  "response": {
    "status": "success",
    "message": "updateEquipmentType",
    "result": {
      "data": [
        [
          {
            "update_status": 1
          }
        ],
        {
          "fieldCount": 0,
          "affectedRows": 0,
          "insertId": 0,
          "serverStatus": 2,
          "warningCount": 0,
          "message": "",
          "protocol41": true,
          "changedRows": 0
        }
      ]
    }
  }
}
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54
-3

An alternative way is as follow, this is through a POST call though

curl --data status=closed https://api.viafoura.com/v2/dev.viafoura.com/pages/7000000043515?verb=PATCH 

I am guessing this is not a general way and only applies to this specific instance.

sheidaei
  • 9,842
  • 20
  • 63
  • 86
  • 2
    This goes against every REST principle. You are losing the benefits of the http PATCH (reducing memory, bandwidth of requests etc). – Ron Jun 11 '14 at 09:59