Getting 400-bad request error, when post data via table rest api in powershell

zhanglixin
Tera Expert

I try to post data from one PDI to another using powershell(the script is copyed from the rest api explore page). 

 

1.I try to get data from one instance, the data return correctly.

zhanglixin_0-1704764683326.png

 

2.Then I edit the record in returned json and use as the body for the post method to post to another instance.

zhanglixin_2-1704764848569.png

 

3.when I run the powershell script, it returns 400 error code.

zhanglixin_3-1704764933225.png

 

4. However when I run the post method seperately. It runs OK.
I have confirmed the body data format is exactly the same.

zhanglixin_4-1704765034662.png

 

Anyone knows why this happens???


1 ACCEPTED SOLUTION

zhanglixin
Tera Expert

hi guys, thanks for replying. I have found the mistake.(I modified the script lightly.)
As I use a rest api to get some records from one instance and want to user import set rest api to post the other.
so I have to loop the records  as a string  and then  convert the string record to json to use as a body.
so I add the "$body1 = $item | convertto-json" sentence. The result runs OK.

zhanglixin_0-1704846722526.png

 

View solution in original post

5 REPLIES 5

Amit Verma
Kilo Patron
Kilo Patron

Hi @zhanglixin 

 

Can you please post your consolidated script here ? Also, please make use of PowerShell ISE while editing PowerShell scripts as other editors unnecessarily introduces some special characters which create problems afterwards,

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Amit Verma
Kilo Patron
Kilo Patron

Hi @zhanglixin 

 

One more observation - You are converting the response received from a GET call to JSON. This is not required as the response is already in JSON. Request you to remove |Convertto-JSON and try once more.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Ankur Bawiskar
Tera Patron
Tera Patron

@zhanglixin 

something wrong with the request body

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yes ,you are right. I missed to convert the string record to json. I corrected the script and post the answer. you can refer it if you are interested.