Pergunta

I have tried everything I can think of and can't get line breaks to go along with the addticketreply command to the API of WHMCS. It's coming from a window's application not PHP or anything. Simple query string, all characters are included before the first line break. After that line break nothing else of the message is included.

I've tried a urlencode method, no go, the API just inserts all text including the encoded part ha ha and doesn't unencode. I've tried replacing line breaks \n with <br> but still WHMCS just shows the <br> ha ha

Any clues how to get full messages through the API including new lines? Documentation is really lacking on this one.

Also the variable adminusername doesn't work at all! I've tried a full name, nope, I've tried username and userid and nope, still inserts my logged in username for ticket replies. The user I am using is a super admin with full access.

The query string for the API looks like this:

accesskey=key&adminusername=Name of Person&ticketid=488&message=asdf asdf
Nothing past that first line goes :(
sad sad sad&action=addticketreply&responsetype=json&username=admin&password=mypassword

I should mention I am using Indy for the Post. Everything else works, any other command passed works, heck even the ticket is updated, the message is updated. It just won't go past that first line break.

EDIT CODE: As I mentioned above I DO let IdHTTP encode my text using the post params, so it should work, AND as I also mentioned above I have tried to pre-encode it, with the results being that WHMCS won't un-encode then and just shows the %20's and other's. Since it's been asked I should mention, I am using Indy 10! So I am up to date with the most recent version.

j := TStringList.Create;
ret := TStringStream.Create('');
j.Text := k+params+'action='+method+'&responsetype=json&username='+username1.Text+'&password='+password;
try
  htp1.Post('http'+s+'://'+url1.Text+'/includes/api.php', j, ret);
except
  on E: EIdHTTPProtocolException do
       result := '{"result":"error", "message":"'+htp1.ResponseText+'"}';
end;
Foi útil?

Solução

Line breaks, spaces, and other reserved characters need to be URL-encoded, eg:

accesskey=key&adminusername=Name%20of%20Person&ticketid=488&message=asdf%20asdf%0D%0ANothing%20past%20that%20first%20line%20goes%20%3A%28%0D%0Asad%20sad%20sad&action=addticketreply&responsetype=json&username=admin&password=mypassword

If you are sending the values via a POST request, then make sure you are usiug an up-to-date version of Indy. TIdHTTP.Post(TStrings) handles the encoding for you, eg:

var
  Params: TStringList;
begin
  Params := TStringList.Create;
  try
    Params.Add('accesskey=key');
    Params.Add('adminusername=Name of Person');
    Params.Add('ticketid=488');
    Params.Add('message=asdf asdf'+CRLF+'Nothing past that first line goes :('+CRLF+'sad sad sad');
    Params.Add('action=addticketreply');
    Params.Add('responsetype=json');
    Params.Add('username=admin');
    Params.Add('password=mypassword');
    IdHTTP1.Post(URL, Params);
  finally
    Params.Free;
  end;
end;

However, if you are sending the values via a URL query string, then you have to manually encode the URL, TdHTTP will not handle that for you, eg:

var
  URL: String;
begin
  URL := 'http://host/path?accesskey=key&adminusername=Name of Person&ticketid=488&message=asdf asdf'+CRLF+'Nothing past that first line goes :('+CRLF+'sad sad sad&action=addticketreply&responsetype=json&username=admin&password=mypassword';
  IdHTTP1.Get(TIdURI.URLEncode(URL));
end;

Or:

var
  URL: String;
  Params: TStringList;
  I: Integer;
begin
  Params := TStringList.Create;
  try
    Params.Add('accesskey=key');
    Params.Add('adminusername=Name of Person');
    Params.Add('ticketid=488');
    Params.Add('message=asdf asdf'+CRLF+'Nothing past that first line goes :('+CRLF+'sad sad sad');
    Params.Add('action=addticketreply');
    Params.Add('responsetype=json');
    Params.Add('username=admin');
    Params.Add('password=mypassword');

    for I := 0 to Params.Count-1 do
      Params[i] := TIdURI.ParamsEncode(Params[i]);

    Params.Delimiter := '&';
    Params.StrictDelimiter := True;
    Params.Quotechar := #0;

    URL := 'http://host/' + TIdURI.PathEncode('path') + '?' + Params.DelimitedText;
    IdHTTP1.Get(URL);
  finally
    Params.Free;
  end;
end;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top