I have a webapp developped in NextJS with the domain quinck.io ( https://www.quinck.io/ ).
I have the need to serve a static JSON file from the path quinck.io/apple-app-site-association with the header Content-Type: application/json.
Right now I've inserted the file apple-app-site-association, which is a JSON file without the .json ( it is a requirement from Apple ), right now Chrome will download the file since it's not served as application/json ( try it from https://quinck.io/apple-app-site-association )
It is possible to achieve the same returning the JSON content ( example: https://reddit.com/apple-app-site-association )
What I've tried:
Custom headers for that path but it still being served as application/octet-stream
async headers() {
return [
{
source: '/apple-app-site-association',
locale: false,
headers: [
{
key: 'Content-Type',
value: 'application/json',
}
],
},
]
}
Rerouting to custom API endpoint returning JSON response
async rewrites() {
return [
{
source: '/apple-app-site-association',
destination: '/api/apple-app-site-association'
}
]
}
None of this above solution did work.
async rewrites() {
return [
{
source: '/apple-app-site-association',
destination: '/api/apple-app-site-association'
}]
This is the correct solution.
It didn't work before because I had the file apple-app-site-associationinside the public folder.
Right now it only works locally, cannot fully understand why. When I upload it on Amplify it doesn't work anymore.
Related
I am building a project using NextJs and Vercel, but, when the users try to access a new page or route, the Vercel gives them the 404 error.
In other projects, I used Netlify as router and this error was fixed using the netlify.toml config file, but, I am not able to do the same using the vercel.json file.
Can you guys help me to turn this file:
netlify.toml
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
Into a vercel.json config file?
I was trying with this settings:
vercel.json
{
"rewrites": [{ "source": "/(.*)", "destination": "/index.html"}]
}
But it did not solved my issue.
A workaround is to use a catch all route that immediately redirects to the index page. For example:
// [...404].jsx
export default function Page() {
return null;
}
export function getServerSideProps() {
return {
redirect: {
destination: '/',
permanent: false,
},
};
}
I am using Axios to load a static JSON from the React application server, and upon reading the files, there are many key/value pairs missing.
The JSON file in question is asset-manifest.json, which includes all the assets (media, css, js) files in the app, and is generated in the build process by webpack. I use this file as reference to pre-load the images for the website.
The JSON file looks like this (ellipsis added by me, ofc):
{
"files": {
"main.js": "/static/js/main.04cbf801.chunk.js",
"main.js.map": "/static/js/main.04cbf801.chunk.js.map",
"runtime-main.js": "/static/js/runtime-main.ed6fc4d3.js",
"runtime-main.js.map": "/static/js/runtime-main.ed6fc4d3.js.map",
"static/css/2.f5a05697.chunk.css": "/static/css/2.f5a05697.chunk.css",
"static/js/2.448c4959.chunk.js": "/static/js/2.448c4959.chunk.js",
"static/js/2.448c4959.chunk.js.map": "/static/js/2.448c4959.chunk.js.map",
"static/css/3.d7d661be.chunk.css": "/static/css/3.d7d661be.chunk.css",
"static/js/3.1605bce2.chunk.js": "/static/js/3.1605bce2.chunk.js",
"static/js/3.1605bce2.chunk.js.map": "/static/js/3.1605bce2.chunk.js.map",
"index.html": "/index.html",
"precache-manifest.093a3239b1d438e30c3887d109b2e7b2.js": "/precache-manifest.093a3239b1d438e30c3887d109b2e7b2.js",
"service-worker.js": "/service-worker.js",
"static/css/2.f5a05697.chunk.css.map": "/static/css/2.f5a05697.chunk.css.map",
"static/css/3.d7d661be.chunk.css.map": "/static/css/3.d7d661be.chunk.css.map",
"static/media/ANKLES.6d795be0.svg": "/static/media/ANKLES.6d795be0.svg",
"static/media/ARMS.8418bd6e.svg": "/static/media/ARMS.8418bd6e.svg",
(...)
},
(...)
}
And there is nothing extraordinary in the key/value pairs that are missing. In total, these are about 15 missing entries, from a total of more than 400. For example, these are some of the key/value pairs missing:
"static/media/testa.png": "/static/media/testa.0b92a7e4.png",
"static/media/Osso01.png": "/static/media/Osso01.9949a55e.png",
"static/media/CabeloHomem01.png": "/static/media/CabeloHomem01.258b6c19.png"
This is the code I am using to load the JSON:
axios.get("asset-manifest.json")
.then(manifest => {
// entries in manifest are already missing at this point
// do stuff with manifest
})
I tried using fetch instead of Axios, for the same result.
BTW, if I copy the JSON code and parse it in the console/terminal, all entries are parsed as expected.
As a workaround, I can embed the related images in the code using base 64, but that's a last resort that I'd prefer to avoid.
Any ideas?
Thanks for reading.
is working fine with fetch first check your response in the postman or any other that you like
let payload = {
token: 'p1ztA-3ZEYrX2wJAa_juSg',
data: {
'static/media/testa.png': '/static/media/testa.0b92a7e4.png',
'static/media/Osso01.png': '/static/media/Osso01.9949a55e.png',
'static/media/CabeloHomem01.png':
'/static/media/CabeloHomem01.258b6c19.png',
'main.js': '/static/js/main.04cbf801.chunk.js',
'main.js.map': '/static/js/main.04cbf801.chunk.js.map',
'runtime-main.js': '/static/js/runtime-main.ed6fc4d3.js',
'runtime-main.js.map': '/static/js/runtime-main.ed6fc4d3.js.map',
'static/css/2.f5a05697.chunk.css': '/static/css/2.f5a05697.chunk.css',
'static/js/2.448c4959.chunk.js': '/static/js/2.448c4959.chunk.js',
'static/js/2.448c4959.chunk.js.map': '/static/js/2.448c4959.chunk.js.map',
'static/css/3.d7d661be.chunk.css': '/static/css/3.d7d661be.chunk.css',
'static/js/3.1605bce2.chunk.js': '/static/js/3.1605bce2.chunk.js',
'static/js/3.1605bce2.chunk.js.map': '/static/js/3.1605bce2.chunk.js.map',
'index.html': '/index.html',
'precache-manifest.093a3239b1d438e30c3887d109b2e7b2.js':
'/precache-manifest.093a3239b1d438e30c3887d109b2e7b2.js',
'service-worker.js': '/service-worker.js',
'static/css/2.f5a05697.chunk.css.map':
'/static/css/2.f5a05697.chunk.css.map',
'static/css/3.d7d661be.chunk.css.map':
'/static/css/3.d7d661be.chunk.css.map',
'static/media/ANKLES.6d795be0.svg': '/static/media/ANKLES.6d795be0.svg',
'static/media/ARMS.8418bd6e.svg': '/static/media/ARMS.8418bd6e.svg',
},
};
fetch('https://app.fakejson.com/q', {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then(response => response.json())
.then(json => console.log(json));
I can't see an answer to this in the Developer's Guide, though maybe I'm not looking in the right place.
I want to intercept HTTP requests with a Chrome Extension, and then forward it on, potentially with new/different HTTP headers - how can I do that?
PS: I am the author of Requestly - Chrome/Firefox extension to modify HTTP requests & responses.
It was certainly not possible when OP asked the question but now you can use WebRequest API with Manifest V2 and DeclarativeNetRequest API with Manifest V3 to write your own extension to modify Request & Response Headers.
Manifest V2 code
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === 'User-Agent') {
details.requestHeaders.splice(i, 1);
break;
}
}
return { requestHeaders: details.requestHeaders };
},
{urls: ['<all_urls>']},
['blocking', 'requestHeaders' /* , 'extraHeaders' */]
// uncomment 'extraHeaders' above in case of special headers since Chrome 72
// see https://developer.chrome.com/extensions/webRequest#life_cycle_footnote
);
Google Chrome is deprecating webRequest Blocking APIs in the Manifest V3. As per the official statement from Google on 28th Sep 2022, all extensions with Manifest v2 won't run on Chrome from June 2023 onwards. Here's an approach to Modify Request & Response headers with Manifest v3 - https://github.com/requestly/modify-headers-manifest-v3
Manifest V3 Code:
rules.ts
const allResourceTypes =
Object.values(chrome.declarativeNetRequest.ResourceType);
export default [
{
id: 1,
priority: 1,
action: {
type: chrome.declarativeNetRequest.RuleActionType.MODIFY_HEADERS,
requestHeaders: [
{
operation: chrome.declarativeNetRequest.HeaderOperation.SET,
header: 'x-test-request-header',
value: 'test-value',
},
]
},
condition: {
urlFilter: '/returnHeaders',
resourceTypes: allResourceTypes,
}
},
{
id: 2,
priority: 1,
action: {
type: chrome.declarativeNetRequest.RuleActionType.MODIFY_HEADERS,
responseHeaders: [
{
operation: chrome.declarativeNetRequest.HeaderOperation.SET,
header: 'x-test-response-header',
value: 'test-value',
},
]
},
condition: {
urlFilter: 'https://testheaders.com/exampleAPI',
resourceTypes: allResourceTypes,
}
},
];
background.ts
import rules from './rules';
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: rules.map((rule) => rule.id), // remove existing rules
addRules: rules
});
The complete source code is available in the GitHub repo - https://github.com/requestly/modify-headers-manifest-v3
If you want to use an existing Chrome/Firefox/Edge Extension, you can use Requestly which allows you to modify request and response headers. Have a look at this snapshot:
Modifying request headers ( https://developer.chrome.com/extensions/webRequest ) is supported in chrome 17.
You are looking at the right place, but intercepting HTTP requests does not exist yet, but the extension team is aware that it's a popular request and would like to get to it sometime in the near future.
Keep in mind that starting from chrome 72, some headers are not allowed unless you add extraHeaders in opt_extraInfoSpec
So the above example in #sachinjain024's answer will look something like this:
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === 'User-Agent') {
details.requestHeaders.splice(i, 1);
break;
}
}
return { requestHeaders: details.requestHeaders };
},
{urls: ['<all_urls>']},
[ 'blocking', 'requestHeaders', 'extraHeaders']
);
For more info, check the documentation Screenshot from the documentation https://developer.chrome.com/extensions/webRequest#life_cycle_footnote
For extensions using manifest version 3, you can no longer use chrome.webRequest.onBeforeSendHeaders.*. The alternative is chrome.declarativeNetRequest
Make the following changes in your manifest.json:
{
...
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"host_permissions": ["<all_urls>"],
"permissions": [
"declarativeNetRequest"
],
...
}
💡 "<all_urls>" is for modifying all outgoing urls's headers. Restrict this for your scope of your work
Make the following changes in your background.js:
// ...
const MY_CUSTOM_RULE_ID = 1
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [MY_CUSTOM_RULE_ID],
addRules: [
{
id: MY_CUSTOM_RULE_ID,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [
{
operation: "set",
header: "my-custom-header",
value: "my custom header value"
}
]
},
condition: {
"resourceTypes": ["main_frame", "sub_frame"]
},
}
],
});
Result
Read the docs https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/
You could install ModHeader extension and add headers:
You can use WebRequest API which is now deprecated it allows you to modify request/response headers.
You can upgrade your extension to Manifest V3 to be able to use DeclativeNetRequest which also supports modifying request/response headers.
Or you can install Inssman chrome extension.
It allows you to modify HTTP(S) request/response headers, reqirect and block request, return custom data like HTML/CSS/JS/JSON and more.
And it is open source project
In my MVC4 web app I'm using FineUploader to upload files.
When a file is uploaded I send a json response from my controller like so:
return this.Json(packageUploadResult, "text/plain", System.Text.Encoding.UTF8);
I had to modify "application/json" to "text/plain" because I was getting "Save As" "Open" options in IE (info here). ONLY IN IE. It works fine in all other browsers.
View:
...
}).on('complete', function (event, id, filename, json) {
if (json.success) {
if (json.IsSignature) {
alert("IN");
}
...
How can I parse Json response to make it work in IE:
sample response:
{"PackageErrorType":0,"PackageId":"AGI-MM-CFG-NUB-2.0.1.2.2","SignatureMatch":false,"IsSignature":false,"success":true}
You could parse the text/plain returned by the server using the $.parseJSON method:
var json = $.parseJSON(textReturnedByServer);
if (json.success) {
if (json.IsSignature) {
alert("IN");
}
}
Of course you should do this only for the legacy browsers that do not support HTML5 File API such as Internet Explorer.
I am trying to use the google drive api to copy a file into a folder
As the documentation says i am generating a POST request to https://www.googleapis.com/drive/v2/files/{fileId}/copy?access_token={MY_ACCESS_TOKEN}
i am using python to generate the request and in order to specify the target folder i am setting the request body to -
{
"parents":
[
{
"id":"{parent_id}"
}
]
}
so the final code is like-
url = https://www.googleapis.com/drive/v2/files/Ansjd021-23123414kjn2k3jn2/copy?access_token=Ya.asdsakjn2312k3n21kj3n-12312j3knb123l
body = {
"parents":
[
{
"id":"ZXC12312323123-Blahasd12323X"
}
]
}
request2 = urllib2.Request(url, urllib.urlencode(body))
request2_open = urllib2.urlopen(request2)
response = request2_open.read()
request2_open.close()
print response
The problem being that I am getting a Http Error : 404 Bad Request
I cant seem to figure out the problem with this. I am doing the same thing using the google playground and its running there.
I did this using https://developers.google.com/drive/v2/reference/files/copy#try-it and got ...
POST https://www.googleapis.com/drive/v2/files/1234/copy?key={YOUR_API_KEY}
Content-Type: application/json
Authorization: Bearer ya29.AHES6ZSZB-t4b4HsR5
X-JavaScript-User-Agent: Google APIs Explorer
{
"parents": [
{
"id": "1234"
}
]
}
This looks pretty much the same as your sample, except the authtoken is in an http header rather than the URL. Also, what happens if you don't urlencode the body? Is there a longer error message in the response body?