Help Nettalk, PUT - Bad Request

Hello,

I’m using Nettalk to change records in REST API, added record with POST and when I change it with PUT it presents error: 400 - Bad Request.
I’m using npostman for testing and it works perfectly, below the information used with NetTalk.

json - POST - OK
{
“razao_social”:“7cd4510fb8784e23”,
“nome_fantasia”:“d2a88ea1d37a4b44”,
“tipo”:“J”,
“cnpj”:“90775490000190”,
“inscricao_estadual”:"",
“suframa”:"",
“rua”:"",
“numero”:"",
“complemento”:"",
“cep”:"",
“bairro”:"",
“cidade”:"",
“estado”:“RS”,
“observacao”:"(id:1/idERP:0);",
“emails”:[
],
“telefones”:[
],
“contatos”:[
],
“enderecos_adicionais”:[
],
“nome_excecao_fiscal”:"",
“excluido”:false
}

json - PUT - Error 400
{
“cidade”:“cacapava do sul”,
“tipo”:“J”,
“razao_social”:“f2e45931e8a24654”
}

net1.Init()
net1.ContentType = ‘application/json’
net1.CustomHeader = ‘ApplicationToken: e00bf526-0196-11eb-94ad-d207fc3b6205<13,10>CompanyToken: ea69b414-0195-11eb-aec6-0e34752640b6’
net1.HeaderOnly = 0
net1.ConnectionKeepAlive = 0
net1.put(‘https://sandbox.mercos.com/api/v1/clientes/6003495’, ‘{{“cidade”:“cacapava do sul”,“tipo”:“J”,“razao_social”:“f2e45931e8a24654”}’)

Hello JLHenriques,

I did something similar, POST Json data to a GraphQL API Endpoint. I’ve used jFiles to create the JSON as there was a lot of nested data to process it within a single CString variable. Also I need to ensure UTF8 encoded data to be send, so StringTheory did the trick.

Here the code that worked for me:

DATA
  json                 JSONClass
  str                  StringTheory
  payload              &STRING
  payloadSize          LONG
  !WebClient           NetWebClient   ! Added as Extension to the procedure !

CODE
  WebClient.Start()
  WebClient.HeaderOnly = FALSE
  WebClient.SetAllHeadersDefault()
  WebClient.SetContentType('json')
  WebClient.SetAccept('json')
  WebClient.AsyncOpenTimeOut = 300

  json.Start()
  ! Create your JSON data
  json.SaveString(str, FALSE)
  payload &= str.AnsiToUtf8(str.GetValuePtr(), payloadSize, st:CP_ISO_8859_1)
  WebClient.SetValue('variables', payload, FALSE, 'json')
  WebClient.SetValue('query', 'mutation () {{ }', FALSE, 'json')
  WebClient.SetContentType('multipart')
  WebClient.Post( 'URL-TO-POST-DATA-TO' )
  DISPOSE(payload)

Best regards
Christoph

My problem is with the PUT method, GET/POST worked.

Are you sure you’re using the API correctly?
According to this…
https://mercos-docs-apiary-io.translate.goog/?_x_tr_sl=pt&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=nui,sc#reference/0/clientes/alterar-um-cliente
You change a Customer record using the ID and PUT
If you just pass in the City, Type and Corporate Name then you’re not using the unique indentifier that the system uses to locate that record to change.

Error 400 on the PUT just means it’s not in the correct format

i generate a json file with the information, curl.exe or npostman it works.

example:
curl https://sandbox.mercos.com/api/v1/clientes/6003628 --include --request PUT --header “Content-Type: application/json” --header “ApplicationToken: e00bf526-0196-11eb-94ad-d207fc3b6205” --header “CompanyToken: ea69b414-0195-11eb-aec6-0e34752640b6” --data-binary “@c:\temp\0000000968.json” --output “c:\temp\0000000968_ret.txt”

your code looks fine, and comparable to the Curl example, but begs the question;
What is in c:\temp\0000000968.json ?
How does that compare to your json data you are sending?

Bruce

Hi Bruce,

The json file contains update below, “tipo” and “razao_social” are required, the others only when changed.

{
“nome_fantasia”:“9a558be4931340b7”,
“cnpj”:“33585142000117”,
“tipo”:“J”,
“razao_social”:“56daacad87ed428d”
}

Hello,

I also work with an GraphQL API and I need to quote/escape the quotes with a backslash,
like " = \" for string data (not for the labels!)

That means it should look like this:

net1.put(‘https://sandbox.mercos.com/api/v1/clientes/6003495 ’, ‘{{“cidade”:\“cacapava do sul\”,“tipo”:\“J\”,“razao_social”:\“f2e45931e8a24654\”}’)

And if the value of “tipo” is not a string (tipo:J instead of tipo:‘J’) then
“tipo”:\“J\” sould be ,“tipo”:“J” as it was.

BTW the API I´m working with would need the labels including the ‘:’ and would look like this:
‘{{cidade:\“cacapava do sul\” tipo:J razao_social:\“f2e45931e8a24654\”}’

Hi JL,

Clearly there is some minor difference in play here that I’ve not been able to glean from your posts.
I recommend you come to one of the user group webinars, on a Thursday, so I can show you how to debug this, and also determine what is the magic detail you are forgetting to tell me :slight_smile:

see https://www.capesoft.com/accessories/NetTalkUserGroup.htm

Cheers
Bruce