HTTP POST request API

I am trying to send a POST request to Notion with json body. The request contains a header with three fields.
I get an response HTTP/1.1 400 Bad Request. What am I doing wrong? Everything works through Postman. I also tried to shape the request body as a matlab structure.
My code:
data = '{"parent":{"database_id":"<my_id>"},"properties":{"title":{"title":[{"text":{"content":"hello"}}]}}}';
uri=matlab.net.URI('https://api.notion.com/v1/pages');
method = matlab.net.http.RequestMethod.POST;
header = matlab.net.http.HeaderField('Authorization', 'Bearer <secret_token>',...
'Content-Type', 'application/json',...
'Notion-Version','2021-05-13');
body = matlab.net.http.MessageBody(data);
request = matlab.net.http.RequestMessage(method,header,body);
[response,completedrequest,history] = send(request,uri)

4 comentarios

Reeshabh Ranjan
Reeshabh Ranjan el 12 de Ag. de 2021
@Orkhan Alikhanov could you update the question with the solution containing MATLAB structure as the request body?
Orkhan Alikhanov
Orkhan Alikhanov el 12 de Ag. de 2021
I was able to compose a request using the function urlread2.
Header = http_createHeader({'Authorization', 'Content-Type','Notion-Version'} ,{['token'], 'application/json','2021-05-13'});
url = 'https://api.notion.com/v1/pages';
data = strcat('{my json request}');
response=urlread2(url,'POST', data ,Header);
Thanks!
Reeshabh Ranjan
Reeshabh Ranjan el 13 de Ag. de 2021
@Orkhan Alikhanov its working now, right?
Orkhan Alikhanov
Orkhan Alikhanov el 13 de Ag. de 2021
@Reeshabh Ranjan Yes. Thank you!

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Productos

Versión

R2020b

Etiquetas

Preguntada:

el 6 de Ag. de 2021

Comentada:

el 13 de Ag. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by