POST request_OData REST API
Hello everyone!
I try to execute a post request, but get this error:
{"error":
{"code":
"INVALID_NONCE","message":"A potential security problem was detected. Refresh the
page and try again. If the problem persists, contact your administrator."
}
}
I found this article:
https://www.ptc.com/en/support/article/CS312447
It says that a token is needed, but I already added it and still does not work.
Here is the request that I execute to get the token:
http://server/Windchill/servlet/odata/PTC/GetCSRFToken()
Here is my code for the post request:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class Post {
public static void main(String[] args) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost("http://server/Windchill/servlet/odata/ProdMgmt/Parts");
String json = "{\r\n" +
"\"Name\":\"TestWTPart_001\",\r\n" +
"\"AssemblyMode\": {\r\n" +
"\"Value\": \"separable\",\r\n" +
"\"Display\": \"Separable\"\r\n" +
"},\r\n" +
"\"GatheringPart\" : false,\r\n" +
"\"PhantomManufacturingPart\" : false,\r\n" +
"\"Context@odata.bind\": \"Containers('OR:wt.pdmlink.PDMLinkProduct:48507000')\",\r\n" +
"\"DefaultUnit\":\"kg\",\r\n" +
"\"DefaultTraceCode\":\"X\",\r\n" +
"\"Source\":\"buy\"\r\n" +
"}";
StringEntity jsonEntity = new StringEntity(json);
httpPost.setEntity(jsonEntity);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("wcadmin", "123");
httpPost.setHeader(new BasicScheme().authenticate(credentials, httpPost, null));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("CSRF_NONCE",
"MY-TOKEN");
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
try (InputStream stream = entity.getContent()) {
StringBuilder textBuilder = new StringBuilder();
try (Reader reader = new BufferedReader(new InputStreamReader
(stream, Charset.forName(StandardCharsets.UTF_8.name())))) {
int c = 0;
while ((c = reader.read()) != -1) {
textBuilder.append((char) c);
}
}
System.out.println(textBuilder);
}
}
System.out.println("\n" + response.getStatusLine().getStatusCode());
response.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Tell me, please, what am I doing wrong?
Thank you in advance!
Best Regards!

