4

I'm trying to create a defect using hpalm rest api but I always get "415 Unsupported Media Type"

Here is what I have tried so far:

var postOptions = {
    jar: cookies, // <-- contains all recieved cookies from authentification
    accept: 'application/json',
    'Content-Type': 'application/xml',
    url: almHost + '/rest/domains/' + domain + '/projects/' + project + '/defects',
    body: data
};

request.post(postOptions, function (error, response, body) {
    if(error) return reject(error);
    else resolve(response);
});

Here is the data I'm sending through body:

var data = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'
+ '<Entity Type="defect">'
    + '<Fields>'
        + '<Field Name="user-06">'
        +     '<Value>MD-4493LS</Value>'
        + '</Field>'
        + '<Field Name="name">'
        +     '<Value>Test with reference 2021</Value>'
        + '</Field>'
        + '<Field Name="description">'
        +     '<Value>description</Value>'
        + '</Field>'
        + '<Field Name="priority">'
        +     '<Value>Medium</Value>'
        + '</Field>'
        + '<Field Name="severity">'
        +     '<Value>Critical</Value>'
        + '</Field>'
    + '</Fields>'
+ '</Entity>';

I don't know what's wrong with this code. The authentifiction works fine because I'm able to fetch Alm defects.

Can anyone help me please ?

Amadou Beye
  • 2,538
  • 3
  • 16
  • 37

1 Answers1

4

Your idea is good, but I think the problem lies exactly on who do you operate with postOptions

You have written already :

var postOptions = {
    jar: cookies, // <-- contains all recieved cookies from authentification
    accept: 'application/json',
    'Content-Type': 'application/xml',
    url: almHost + '/rest/domains/' + domain + '/projects/' + project + '/defects',
    body: data
};

But it seems that Content-Type is just there in the object freely. It's not a proper property for now.

Since Content-Type is HTTP header, we need to somehow indicate that. And for request there is a property headers that contains all custom headers you want to add to your request.

Try doing it like that:

var postOptions = {
    jar: cookies, // <-- contains all recieved cookies from authentification
    accept: 'application/json',
    headers: {
         'Content-Type': 'application/xml',
         'Content-Length': Buffer.byteLength(data) 
    }
    url: almHost + '/rest/domains/' + domain + '/projects/' + project + '/defects',
    body: data
};
Emil Hotkowski
  • 2,233
  • 1
  • 13
  • 19
  • 1
    My God. it's so much under my nose. All `content-type` properties are in `headers ` property in other queries. In the Alm documentation, the 415 message is `Unsupported request content type` which is pretty clear. But when getting `Unsupported Media Type` I thought it was a file handling problem. Thank you so much – Amadou Beye Nov 20 '19 at 12:13
  • 1
    This is a typical message with HTTP 415 error. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/415 I know how it is with documentation ,but I'm glad it works now : ) – Emil Hotkowski Nov 20 '19 at 12:18