0
votes

Im tying to use a REST post to send data from a MS SSIS Process:

    json = json + "{ ";
        json = json + "\"fields\": {";
        json = json + "\"project\": {  \"id\": \"XXX\" },";
        json = json + "\"summary\": \"REST ye merry gentlemen.\",";
        json = json + "\"description\": \"Hellow\",";
        json = json + "\"issuetype\": { \"name\": \"Bug\" }";
        json = json + "} ";
        json = json + "} ";


        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://xxx.atlassian.net/rest/api/2/issue/");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";


        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {

            MessageBox.Show(json);

            streamWriter.Write(json);
        }

        string mergedCredentials = string.Format("{0}:{1}", "xxx", "xxx");
        byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
        string credentials = Convert.ToBase64String(byteCredentials);

        //httpWebRequest.Headers.Add("Authorization", "Basic " + credentials);

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();
            //Now you have your response.
            //or false depending on information in the response

        } 

The server responds:

SSIS package "Package.dtsx" starting. Error: 0x1 at Script Task: System.Reflection.TargetInvocationException: Het doel van een aanroep heeft een uitzondering veroorzaakt. ---> System.Net.WebException: De externe server heeft een fout geretourneerd: (400) Ongeldige opdracht. bij System.Net.HttpWebRequest.GetResponse() bij ST_8fbfe559ee824ef19f7c9bc2c425effc.csproj.ScriptMain.Main() --- Einde van intern uitzonderingsstackpad --- bij System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
bij System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) bij Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript() Task failed: Script Task Warning: 0x80019002 at Package: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED. The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors. SSIS package "Package.dtsx" finished: Failure.

The English bits of the error message are

The purpose of a call has caused an exception. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request

1
But i do not understand why this is the case, the jira documentation states that this is the correct code: developer.atlassian.com/static/rest/jira/5.2.5.html#id326535user1114720

1 Answers

0
votes

I tested your JSON with my application and it seems to be correct (assuming your projectId and issueTypeName are correct for your Jira). (EDIT2: Make sure you send all the required fields for the issueType you're creating).

But, why are you not adding Authorization in the header?

Also, I encode my JSON to UTF8 before POSTing it. Not sure if your StreamWriter do the same. (EDIT1: Ok, my apologies, StreamWriter Encode to UTF-8, but my code still differ with request.ContentLength and request.Accept, not sure again if it will make a difference)

byte[] data = Encoding.UTF8.GetBytes(postData);
request.Accept = "application/json";
request.ContentLength = data.Length;
using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(data, 0, data.Length);
}

I could add all my Methods if this snippet doesn't help you.