API POST not working

Hi everyone,

I’m trying to use REST-API to add a new Lead.

I’m successfully using token auth, but when it comes to add a new lead is not working.

As you can see from below screenshot, POST request is behaving like a GET request, it returns the only Lead in ErpNext instead of adding a new one.

How to solve this? I searched on the forum and someone is talking about cookies as solution but I don’t understand how to use them.

CRM-LEAD-2020-00001 is a Lead that I manually added directly in ErpNext.

ERPNext: v12.11.2 (version-12)
Frappe Framework: v12.9.1 (version-12)

Would you use the code button to produce a curl example and post it here?

Hey @mirco, I had this exact problem. In addition to what Martin has suggested, can you try doing this in ARC? curl works if you’re ok with using a curl, but if you want to post pure JSON data into the body, I have had better luck using ARC than using Postman. In my research, I saw a couple of forum posts indicating that Postman uses a special header for form-data, and that 200 OK for ERPNext sometimes means “resource not found,” which is obviously not a great error code to use because it normally means “you did it!”

I find that ARC throws detailed exceptions including the Python args that failed, so I prefer to use it for developing our API with ERPNext in particular. Let me know if you have the same results!

Hi, thank you for your answers.

Here is the curl code produced by POSTMAN

curl --location --request POST ‘https://sub.example.com/api/resource/Lead/’
–header ‘Authorization: token 9efb53433acb7ba*:xxxxxxxxxxxx*’
–header ‘Content-Type: application/json’
–header ‘Cookie: sid=Guest; system_user=yes; full_name=Guest; user_id=Guest; user_image=’
–data-raw ‘[{“notes":“Notes”,“phone”:“123456789”,“email_id”:"exmaple@test.com”,“lead_name”:“John”,“address_desc”:“New York”}]’

With ARC is returning a 301

The get request is working fine

Here is the curl code proced by ARC
curl “https://sub.example.com/api/resource/Lead/”
-X POST
-d “[\n {\n "notes": "Notes",\n "phone": "123456789",\n "email_id": "exmaple@test.com",\n "lead_name": "John",\n "address_desc": "New York"\n }\n]”
-H “Authorization: token 9efb53433acb7ba:xxxxxxxxxxxx
-H “Content-Type: application/json”

It’s really important when posting code fragments to use the forum’s code formatting. In order to test your Postman generated curl I had to search and replace all of these ‘ for these ', all of these “ for these " and all of these – for these --.

In other words, this:
curl --location --request POST ‘https:// sub.example.com /api/resource/Lead/’
–header ‘Authorization: token 9efb53433acb7ba*:xxxxxxxxxxxx*’
–header ‘Content-Type: application/json’
–header ‘Cookie: sid=Guest; system_user=yes; full_name=Guest; user_id=Guest; user_image=’
–data-raw ‘[{“notes":“Notes”,“phone”:“123456789”,“email_id”:"exmaple@test.com”,“lead_name”:“John”,“address_desc”:“New York”}]’

should have been this:

curl --location --request POST 'https://sub.example.com/api/resource/Lead/' \
–header 'Authorization: token ec226d15b52879e:xxxxxxxxxxxxxx' \
–header 'Content-Type: application/json' \
–data-raw '[{"notes":"Notes”,"phone”:"123456789”,"email_id”:"exmaple@test.com”,"lead_name”:"John”,"address_desc”:"New York”}]'

I get the same result as you.

POSTing a Lead does nothing except behave like a GET.

To solve the 301 error you need to investigate the --location switch in curl, then find out how to get ARC to emit that switch. If you remove --location from the Postman generated curl you will get a 301 error.

I have your answer.

Tested and working (substitute your host and token)

curl --silent --location --request POST 'https://sub.example.com/api/resource/Lead' \
--header 'Authorization: token ec226d15b52879e:xxxxxxxxxxxxxxxx' \
--data-raw '{
    "naming_series": "CRM-LEAD-.YYYY.-",
    "lead_name": "Kayleigh McEnany",
    "email_id": "kayleigh.mcenany@ministryoftruth.gov"
}'

The apparent GET, rather than a POST that correctly inserts the lead, is caused by the trailing slash “/” in the URL:

Bad:

curl -sLX POST 'https://sub.example.com/api/resource/Lead/'

Good:

curl -sLX POST 'https://sub.example.com/api/resource/Lead'
3 Likes

I’m so sorry,
i didn’t found the button for posting code, is that preformatted text?

Thank you so much @MartinHBramwell
Is now working fine. Saved my day :slight_smile:

Perhaps you could try it and see what happens.

That’s bad, yup.

Worse – is an API that returns HTML error messages! It’s hard to express in polite terms how bad that is.

HTTP & HTML were created to solve the problem of human beings trying to get at remote data in a friendly way. With that becoming standard, APIs had to be created to solve the problem of software having to parse human friendly info in order to get at the same data.

Frappe’s brilliant step forward has been to insist that software MUST parse human friendly error messages to find out how to react when things going wrong! Feh. That is so last millenium.

1 Like