Skip to main content

Hello.


I would like to update the tracking code in PowerShell, but when I run the following code, I get a 400 error.



$token = "<access token>"

$uri = 'https://api.box.com/2.0/users/<user id>'

$headers = @{"Authorization" = "Bearer " + $token}

$body = [System.Text.Encoding]::UTF8.GetBytes("tracking_codes": [ { "type": "tracking_code", "name":"<tracking code name>",value":"<tracking code value> "}])

Invoke-RestMethod -Method Put -Uri $uri -Headers $headers -Body $body -ContentType 'application/json'



▼Error content



+ Invoke-RestMethod -Method Put -Uri $uri -Headers $headers -Body $body ...



+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) )Invoke-RestMethod], WebException

+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand



Could you please let me know if there is any part of the code that is wrong?

Hi, welcome to the forum.



Just by looking at the code snipet I don’t see anything wrong, but I also cant tell what error it is.



Do you have any extra information, such as did you get back an HTTP error code from the API.



My suggestions is to try that same PUT via Postman just to try to surface the issue.



In the mean time, it does seem you’re trying to use the API directly via PowerShell, and I’m wondering if you considered using the Box CLI with PowerShell.







Let us know.



Cheers


Hi there.


Thank you for your reply.



The HTTP error code returned by the API is as follows







Invoke-RestMethod : The remote server returned an error: (400) Bad Request.


However, the command to change the user name using the same PUT succeeded.


Also, the command to change the user name was successful, and the command execution using Postman was successful.


The Body portion of the successful command is attached to the email.







I considered using the Box CLI with PowerShell, but gave up because I did not know how to change the TrackingCode.


If you could please tell me the command script to run with Box CLI, or the attribute key to include in the CSV file.



I look forward to hearing from you.



2023年8月9日(水) 6:27 Rui Barbosa via Box Developer Community <notifications@box1.discoursemail.com>:




Hi,



My demo account doesn’t have tracking codes enabled, let me ask around.



In the mean time I looked into your example:



$body = =System.Text.Encoding]::UTF8.GetBytes("tracking_codes": : { "type": "tracking_code", "name":"<tracking code name>",value":"<tracking code value> "}])



Lets break it down:



$body = =System.Text.Encoding]::UTF8.GetBytes(

"tracking_codes": :

{

"type": "tracking_code",

"name":"<tracking code name>",

value":"<tracking code value> "

}

]

)



Looking closer to the example there seem to be some issues with the strings and possible a JSON formatting, however I believe it would cause another error type on the command, where the JSON formatting would explain the 400 bad request.



I think it still needs for the entire string to be enclosed in “”, there seems to be a missing " on the values, and I think it needs the { and the } in the start and the end.



Could you try using:



$body = oSystem.Text.Encoding]::UTF8.GetBytes(

"{

"tracking_codes": d

{

"type": "tracking_code",

"name":"<tracking code name>",

"value":"<tracking code value> "

}

]

}"

)



Also I’m not sure how powershell handles quotes inside quotes, so you may need to do something else.


In macOS/Linux we need to escape (")



Let us know if this worked.



Best regards


Hi,



I have succeeded by modifying the command as follows.





$token =“”


$uri = ‘https://api.box.com/2.0/users/


$headers = @{“Authorization” = "Bearer " + $token}


$body = ‘{


“tracking_codes”: r


{


“type”: “tracking_code”,


“name”: “”,


“value”: “”


}]


}’


Invoke-RestMethod -Method Put -Uri $uri -Headers $headers -Body $body -ContentType ‘application/json’



Also, it would be useful if we could change it all at once in a csv file, so could you please give us a sample code to run in BoxCLI and the attribute key information in the csv file?



Thank you in advance.



2023年8月16日(水) 5:40 Rui Barbosa via Box Developer Community <notifications@box1.discoursemail.com>:


Fantastic @user56 , that is what I like to hear!!!



As for the CLI option, I tested it and couldn’t find a way to update the tracking codes.


After speaking to the CLI development team, it turns out it wasn’t implemented yet.



Today I found out that they have released it!!!







I haven’t had a chance to test it my self yet, but feel free to explore it.



Cheers


Thank you for sharing the BoxCLI information!


I tried it and saw that it successfully updated the tracking code!


This is easier and better.



I will wait patiently for the CSV file to be able to update as well.



Thanks a bunch



2023年8月23日(水) 1:12 Rui Barbosa via Box Developer Community <notifications@box1.discoursemail.com>:


Hi,



Consider my tracking codes:




Consider this CSV file:



type,id,tracking_codes

"user","22240548078","LaptopSerial=AAA,PhoneSerial=111"

"user","22240405099","LaptopSerial=BBB,PhoneSerial=222"

"user","22240545678","LaptopSerial=CCC,PhoneSerial=333"

"user","18622116055","LaptopSerial=DDD,PhoneSerial=444"



Using Box v 3.10



❯ box --version

@box/cli/3.10.0 darwin-x64 node-v14.20.1





❯ box users:update --csv --fields name --bulk-file-path user_tracking_codes_b.csv

========================================] 100% | 4/4

type,id,name

user,22240548078,Investment User

user,22240405099,Wealth User

user,22240545678,Wholesale User

user,18622116055,Rui Barbosa

All bulk input entries processed successfully.



Now checking box users:



❯ box users --csv --fields type,id,name,tracking_codes

type,id,name,tracking_codes

user,18622116055,Rui Barbosa,"b{""type"":""tracking_code"",""name"":""LaptopSerial"",""value"":""DDD""},{""type"":""tracking_code"",""name"":""PhoneSerial"",""value"":""444""}]"

user,22240405099,Wealth User,"U{""type"":""tracking_code"",""name"":""LaptopSerial"",""value"":""BBB""},{""type"":""tracking_code"",""name"":""PhoneSerial"",""value"":""222""}]"

user,22240545678,Wholesale User,"U{""type"":""tracking_code"",""name"":""LaptopSerial"",""value"":""CCC""},{""type"":""tracking_code"",""name"":""PhoneSerial"",""value"":""333""}]"

user,22240548078,Investment User,"U{""type"":""tracking_code"",""name"":""LaptopSerial"",""value"":""AAA""},{""type"":""tracking_code"",""name"":""PhoneSerial"",""value"":""111""}]"



Let us know if this helps



Cheers


Hi,



Processing by CSV file was also successful.


All the questions I wanted to ask have been resolved, so please close.



Many thanks



2023年8月24日(木) 6:50 Rui Barbosa via Box Developer Community <notifications@box1.discoursemail.com>:


Reply