title: AJAX post request to third party REST api
body: <p>. I am using laravel backend as API and angular as frontend which calls laravel api. Laravel api access the teamwork api through basic authentication using curl. </p>
  
  <p>Now I am working with teamwork Api. Trying to create a comment using the API.
  API documentation describes following.</p>
  
  <pre><code>    {
    "comment": {
      "body": "Reply to earlier comment",
      "notify": "",
      "isprivate": false,
      "pendingFileAttachments": "",
      "content-type": "TEXT"
    }
  }
  </code></pre>
  
  <p>//ref : <a href="http://developer.teamwork.com/comments#creating_a_commen" rel="nofollow">http://developer.teamwork.com/comments#creating_a_commen</a></p>
  
  <p>in my ajax call i have used following</p>
  
  <pre><code>  var data = $.param({
      'body' : commentBodyValue,  //variable
      'notify': "",
      'isPrivate':false,
      "pendingFileAttachments": "",
      "content-type": "TEXT"
    });
  </code></pre>
  
  <p>My post does not give error, but it also do not create a new comment too. What am I missing? I think i failed to arrange data according to the format allowed in api. Can you kindly help me to fix it? </p>
  
  <h2>Edit:</h2>
  
  <p>Angular Controller:</p>
  
  <pre><code>//add new comment
  $scope.addComment = function(taskId,commentAuthorId)
  {
    var commentBod  = document.getElementById("commentBody");
    var commentBodyValue = commentBod.value;
    console.log("new comment : "+ taskId + commentBodyValue);
    //submit the post request
    //$http.post();
    //using jquery function param to serialize 
    var data = $.param({
      'body' : commentBodyValue,
      'notify': "",
      'isPrivate':false,
      "pendingFileAttachments": "",
      "content-type": "TEXT"
    });
    var config = {
        headers : {
            // 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;',
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    }
    var url = "http://abounde.com/portal/api/post_comment/"+taskId;
    $http.post(url,data,config)
    .then(
      function(response){
        //success
        console.log("response : Submitted :D  " + response);
      },
      function(response){
        //failure
        console.log(response);
      }
    );
  
  }
  </code></pre>
  
  <h2>Edit 2</h2>
  
  <p>Laravel route and controller
  <strong>Routes:</strong>
          //post comment to resource
          Route::post('post_comment/{task_id}',[
              'uses'=>'PortalController@postComment',
              'as'=>'portal.client.postComment'
          ]);</p>
  
  <p><strong>Controller:</strong> </p>
  
  <pre><code>    //post comment at task
          public function postComment(Request $request, $task_id)
          {
              $secretApiKey = $request-&gt;session()-&gt;get('secretApiKey');
              if(!empty($secretApiKey)){
              //post the msg
                  $this-&gt;callTeamworkApi($secretApiKey,"tasks/".$task_id."/comments.json");
  
              }else{
                  return response()-&gt;json(['response'=&gt;"Not Logged In"]);
              }           
          }
  
  
  //authentication method
  
      public function callTeamworkApi($secretApiKey, $apiCallString)
      {
              //cURL
          $password = "xxx";
          $channel = curl_init();
          //options
          curl_setopt($channel, CURLOPT_URL, "http://projects.abounde.com/".$apiCallString); // projects.json?status=LATE gets all late projects
          curl_setopt($channel, CURLOPT_HTTPHEADER,
                      array(
                        "Authorization: Basic " . base64_encode($secretApiKey . ":" . $password)
          ));
  
  
          $msg = curl_exec($channel);
          curl_close($channel);
          return response()-&gt;json(['res'=&gt;$msg]);
  
      }
  </code></pre>
  
  <p>EDIT 3: Contacted with teamwork Api support.
  After their advice i came up with following code</p>
  
  <pre><code>  //post comment at task
      public function postComment(Request $request, $task_id)
      {
          $secretApiKey = $request-&gt;session()-&gt;get('secretApiKey');
          if(!empty($secretApiKey)){
  
          //post the msg
              $comment=array();
              // $comment['body']=$request-&gt;input('body');
              // $comment['notify']=$request-&gt;input('notify');          
              // $comment['isprivate']=$request-&gt;input('isprivate');
              // $comment['pendingFileAttachments']=$request-&gt;input('pendingFileAttachments');
              // $comment['content-type']=$request-&gt;input('content-type');
  
  
              $comment['comment']['body']="test";
              $comment['comment']['notify']="";
              $comment['comment']['isprivate']=false;
              $comment['comment']['pendingFileAttachments']="";
              $comment['comment']['content-type']="text";
  
  
              $this-&gt;callTeamworkPostApi($secretApiKey,"tasks/".$task_id."/comments.json",json_encode($comment));
              //var_dump($comment);
              //return response()-&gt;json(['response'=&gt;"Trying"]);
          }else{
              return response()-&gt;json(['response'=&gt;"Not Logged In"]);
          }           
      }
  }
  </code></pre>
  
  <p>///</p>
  
  <pre><code>  public function callTeamworkPostApi($secretApiKey, $apiCallString,$comment)
  {
          //cURL
      $password = "xxx";
      $channel = curl_init();
      //options
      curl_setopt($channel, CURLOPT_URL, "http://projects.abounde.com/".$apiCallString); // projects.json?status=LATE gets all late projects
      curl_setopt($channel, CURLOPT_HTTPHEADER,
                  array(
                    "Authorization: Basic " . base64_encode($secretApiKey . ":" . $password)
      ));
      curl_setopt($channel, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
      curl_setopt($channel, CURLOPT_POSTFIELDS, $comment);
  
  
      $msg = curl_exec($channel);
      curl_close($channel);
              var_dump($msg);
      //return response()-&gt;json(['res'=&gt;$msg]);
  
  
  }
  </code></pre>
  
  <p>var_dump($comment) gives </p>
  
  <pre><code>string(107) "{"comment":{"body":"test","notify":"","isprivate":false,"pendingFileAttachments":"","content-type":"text"}}"
  </code></pre>
  
  <p>However the api still do not save the comment. It has to be a curl post method issue. Right?</p>
  
  <p>Edit 4:
  I have done a var_dump(curl_getinfo($channel));</p>
  
  <p>this gave me following response.</p>
  
  <pre><code>array(26) { ["url"]=&gt; string(55) "http://projects.abounde.com/tasks/8199861/comments.json" ["content_type"]=&gt; NULL ["http_code"]=&gt; int(500) ["header_size"]=&gt; int(229) ["request_size"]=&gt; int(311) ["filetime"]=&gt; int(-1) ["ssl_verify_result"]=&gt; int(0) ["redirect_count"]=&gt; int(0) ["total_time"]=&gt; float(0.5) ["namelookup_time"]=&gt; float(0) ["connect_time"]=&gt; float(0.109) ["pretransfer_time"]=&gt; float(0.109) ["size_upload"]=&gt; float(107) ["size_download"]=&gt; float(0) ["speed_download"]=&gt; float(0) ["speed_upload"]=&gt; float(214) ["download_content_length"]=&gt; float(0) ["upload_content_length"]=&gt; float(107) ["starttransfer_time"]=&gt; float(0.5) ["redirect_time"]=&gt; float(0) ["redirect_url"]=&gt; string(0) "" ["primary_ip"]=&gt; string(13) "23.23.184.208" ["certinfo"]=&gt; array(0) { } ["primary_port"]=&gt; int(80) ["local_ip"]=&gt; string(11) "192.168.0.6" ["local_port"]=&gt; int(31657) }
  </code></pre>
  
  <p>The response code 500 may points to the fact that teamwork has some kind of internal issues.</p>