Skip to main content
11-Garnet
March 22, 2023
Solved

Problem getting CSRF_NONCE value (HTTP 400)

  • March 22, 2023
  • 2 replies
  • 4562 views

Hello,

I am stuck and need suggestions what to do. I am trying to get CSRF_NONCE value, using rest query with java api (I working with Windchill 12.0 server). But what ever I do I get HTTP code 400.

Tried queries were: https://myServer/Windchill/servlet/odata/PTC/GetCSRFToken()

My code is:

URL url = new URL ("https://myServer/Windchill/servlet/odata/PTC/GetCSRFToken()");
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "Basic " + authString); //authstring is Base64 encoded
conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
 response.append(inputLine);
}
in.close();
conn.disconnect();

------------
I am pretty sure that code is ok because if I try query "https://myServer/Windchill//servlet/rest/objects?$select=number&typeId=wt.part.WTPart"
I am getting right result. Also both queries return right result if I use Firefox or Postman instead of my
code.

If anybody have suggestions what is wrong or what to do I will be grateful for help.
Best reagads,
Nenad

 

 

 

 

 

 

 

 

Best answer by HelesicPetr

Hi @nbojcetic 

You forgot to add a accept property string

 

conn.setRequestProperty("accept", "application/json");

 

I test it and it works

 

PetrH

2 replies

HelesicPetr
22-Sapphire II
22-Sapphire II
March 22, 2023

Hi @nbojcetic 

May be the version does miss in the url

https://wchserver.aa.com:443/Windchill/servlet/odata/v3/PTC/GetCSRFToken()

 PetrH

PetrH
nbojcetic11-GarnetAuthor
11-Garnet
March 22, 2023

PetrH,

thank you for the suggestion, I already tired that but with the same result.

 

Thanks!

HelesicPetr
22-Sapphire II
22-Sapphire II
March 22, 2023

Hi @nbojcetic 

Have you tried earlier versions? v1 ? v2?

 

go to the a restApi documentation and test it directly from the windchill if it works.

https://wch.server.com/Windchill/netmarkets/html/wrs/doc.html

HelesicPetr_0-1679495061569.png

 

PetrH

PetrH
HelesicPetr
22-Sapphire II
22-Sapphire II
March 22, 2023

Hi @nbojcetic 

You forgot to add a accept property string

 

conn.setRequestProperty("accept", "application/json");

 

I test it and it works

 

PetrH

PetrH
nbojcetic11-GarnetAuthor
11-Garnet
March 22, 2023

YES, that is correct and now it works. Thank you very much! 😊

 

Nenad