Insert rows in an empty data extension on salesforce marketing cloud by using API call - api

I didn't achieve to receive data in my data extension (empty) by using the API multiple data extension rows method.
I created a data extension with 7 variables (6 data type = text, 1 is a date). One of the 7 variables is a primary key (text data type).
On postman, I created a first POST request:
URL: https://mc08wx0hkc21mnr8g402m7q36hy4.auth.marketingcloudapis.com/v2/token
In the body:
{
"client_id": " ",
"client_secret": " ",
"account_id": " ",
"grant_type": " "
}
With all the information about the package that I created on marketing cloud and that I have to use:
(scope : data extension => read, write)
By sending this request, I got my access_token.
The second part is more confused for me, I have to make the connection so as to populate my data extension.
I created a second POST request:
In the body:
Host : https://MY_SUBDOMAIN.rest.marketingcloudapis.com
POST /hub/v1/dataevents/key: ID DataExtension/rowset
Content-Type: application/json
Authorization: Bearer eyJhY2Nlc...
The part with the URL is more confused for me. As I said before, I have one primary key in the data extension that I created to receive the datas
URL: https://mc08wx0hkc21mnr8g402m7q36hy4.rest.marketingcloudapis.com/ POST /hub/v1/dataevents/key: EXTERNAL_KEY_OF_MY_DATA_EXTENSION/keys :MY_PRIMARY_KEY_NAME/
I followed the Salesforce docs.
By sending the request, I had:
<h1>596 Service Not Found</h1>

Related

How to Emulate a POST Request from PostMan - For a series of Integration Tests in a WebAPI Development

I am using PostMan as part of the testing regime for a WebAPI service development that I am working on. In order for external parties to gain access to our WebAPI service they first need to obtain an access token.
The POST request returns some JSON containing the required access token:
{
"access_token": "anencryptedaccesstoken",
"scope": "am_application_scope default",
"token_type": "Bearer",
"expires_in": 3218
}
I am putting together a series of integration tests which need to emulate the POST calls from POSTMAN. I am currently using System.Net.WebClient to achieve this. I am not sure what I need to do in order to achieve my goal. Here is a function that I am using to try and obtain the access token:
Public Shared Function GetAccessToken(ByVal endpoint As String, wc As WebClient) As String
Dim result As String = ""
Dim data As Byte() = Nothing
'Header information
wc.Headers.Add("Authorization", "Basic <alongencryptedstring>")
wc.Headers.Add("Content_Type", "application/x-www-form-urlencoded")
result = wc.UploadString(endpoint, "POST", "")
Return result
End Function
The 'Body' tab in PostMan contains the following entries:
grant_type - client_credentials
Content_Type - application%2Fx-www-form-urlencoded
In this instance as far as I am aware there is no 'data' element to the PostMan request hence the empty string in my use of UploadString. The function returns the following error:
"The remote server returned an error: (415) Unsupported Media Type."
I am new to web app development so please bear with me, I am finding it difficult to phrase what I think are meaningful question in the context of this post, I hope however that I have ssupplied sufficient information to convey the gist of my problem and for someone to be able to respond appropriately.
Kind Regards
Paul.

Attribute Error : 'Request' object has no attribute 'headers'- Django

I am using the django 1.11 version and django rest framework for the rest api
I am passing the token value in the HTTP header in React Native using fetch
But when I am trying to retrieve the token value in django views file it is giving me error
In react Native I am passing the token value as below
fetch(url,{
method: 'get',
headers : new Headers({
'token':'token',
'Content-Type': 'application/json'
})
})
I django rest APi I am trying to fetch the token value as below
def get(self,request,**kwargs):
token = request.headers['token']
queryset=models.Schedule.objects.filter()
serializer_class= RepScheduleSerializer(queryset,many=True)
return Response(serializer_class.data)
But it is giving me error
Request object has no attribute headers
I want to fetch the token value in the function
The headers of a request are stored in the request.META dictionary [Django-doc]. You thus should alter the code to:
def get(self,request,**kwargs):
token = request.META['HTTP_TOKEN']
queryset = models.Schedule.objects.all()
serializer_class = RepScheduleSerializer(queryset,many=True)
return Response(serializer_class.data)
Right now however, you do not do anything with this token. You thus might need to alter the logic.
Note that the keys are pre-processed:
With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.
Since django-2.2, there is the request.headers dictionary-like object [Django-doc], that allows case-insensitive lookups. Based on the error message, you however do not use django-2.2.

Softlayer GET API "VirtualGuests" response too big

I am trying to do a GET operation on
GET https://api.softlayer.com/rest/v3/SoftLayer_Account/VirtualGuests?objectMask=powerState%3BoperatingSystem.passwords%3Bdatacenter%3BbillingItem%3BblockDevices.diskImage%3BtagReferences
> Headers: Authorization: Accept : application/json
Response:
{
"error": "Internal Error",
"code": "SoftLayer_Exception_Public"
}
I found out that the results is too big, If I do a resultLimit on the API it works.
My question is, I have instance Id with me how Can I include it in the above API. I tried adding &id=XXXXXX at the end of the API it didn't work
I think you want to get only the data which contains the instance with ID correct?
well if you want the VM whose id = instace ID you can use the Softlayer_Virtual_Guest::getObject method instead Softlayer_Account::getVirtualGuests
http://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/getObject
If you are looking for something more complex you can try objectFilters
https://sldn.softlayer.com/article/object-filters
if you have more questions let me know.

Getting response as 'Unauthorized' while sending access token via URI Query Parameter

We are creating REST API and implemented oAuth 2, using YII framework.
We are facing a strange issue, while we are trying to access the resource and sending access token via "Authorization Request Header Field" we are getting the expected output.
e.g.
curl -i -H "Accept:application/json" -H "Authorization: Bearer XXXXXX"
Whereas while we are trying to send the access token via "URI Query Parameter" we are getting response as "Unauthorized".
e.g.
https://server.example.com/resource?access_token=XXXXXX&p=q
Your suggestions would be really helpful for us.
RFC 6750 (Bearer Token Usage) defines 3 ways to pass an access token to a protected resource endpoint.
Via Authorization header. (2.1. Authorization Request Header Field)
Via a form parameter access_token. (2.2. Form-Encoded Body Parameter)
Via a query parameter access_token. (2.3. URI Query Parameter)
Among the above, only the first way is mandatory. It seems your authorization server does not support the third way.
Addition for the comment
Below is an example to support all the 3 ways in PHP. See "3. Extract Access Token" in "Protected Resource" for details and for other examples in Ruby and Java.
/**
* Function to extract an access token from a request.
*/
function extract_access_token()
{
// The value of Authorization header.
$header = $_SERVER['HTTP_AUTHORIZATION'];
// If the value is in the format of 'Bearer access-token'.
if ($header != null && preg_match('/^Bearer[ ]+(.+)/i', $header, $captured))
{
// Return the value extracted from Authorization header.
return $captured;
}
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
// Return the value of 'access_token' query parameter.
return $_GET['access_token'];
}
else
{
// Return the value of 'access_token' form parameter.
return $_POST['access_token'];
}
}
I don't know Yii, but my guess is that simply the framework does not contain code like the above.

File Upload with Additional Parameters as JSON

I am using the below code to upload image with some additional parameters.
[HttpPost]
public HttpResponseMessage Upload(Data data)
{
var count = HttpContext.Current.Request.Files.Count;
return null;
}
I tried checked this method using postman chrome extension. I passed these values
Under Headers
enctype: multipart/form-data
Content-Type: application/json
Under form-data added one image file
Under raw
{
"Id": "1",
"ModifiedBy" : "1"
}
But the problem i am getting 0 for count
The Content-Type should be multipart/form-data not application/json for your code to work. A typical content type looks like this:
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvmxBBVAoH3KRsa9L
The actual post data then contains blocks of 'data' separated by the boundary. These blocks can contain a content type, but they don't have to. If you're doing file uploads the content type should not matter - the filename is what determines the type.
All that said, with WebAPI you shouldn't be using the ASP.NET HttpContext object, but rather the lower level Web API semantics. While what you're doing works as long as you run inside of the ASP.NET stack, if you self-host or maybe in the future run ontop of a different stack like OWin/Katana this code will no longer work.
To handle file uploads with Web API specific code check out this blog post from Filip W.
http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/